Advertisement
housec84

SafeBitmap

Jul 27th, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. public static Bitmap SafeBitmap(Bitmap bmp)
  2.         {
  3.             //make output bmp.
  4.             Bitmap b = new Bitmap(bmp.Width, bmp.Height);
  5.             //get input bmp data for input and lock input bmp in memory
  6.             BitmapData bmpD = bmp.LockBits(
  7.                 new Rectangle(0, 0, bmp.Width, bmp.Height),
  8.                 ImageLockMode.ReadOnly,
  9.                 bmp.PixelFormat);
  10.             //get output bmp data for input and lock output bmp in memory
  11.             BitmapData bD = b.LockBits(
  12.                 new Rectangle(0, 0, bmp.Width, bmp.Height),
  13.                 ImageLockMode.WriteOnly,
  14.                 bmp.PixelFormat);
  15.             //make temp storage array.
  16.             byte[] tempBytes = new byte[Math.Abs(bD.Stride) * b.Height];
  17.             //get pointers to bitmap data.
  18.             IntPtr ptrNew = bD.Scan0;
  19.             IntPtr ptrOld = bmpD.Scan0;
  20.             //copy input data to storage array.
  21.             Marshal.Copy(ptrOld, tempBytes, 0, tempBytes.Length);
  22.             //copy storage array to outputdata
  23.             Marshal.Copy(tempBytes, 0, ptrNew, tempBytes.Length);
  24.             //unlock bitmaps.
  25.             b.UnlockBits(bD);
  26.             bmp.UnlockBits(bmpD);
  27.             //dispose input bitmap.
  28.             bmp.Dispose();
  29.             return b;
  30.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement