Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Bitmap SafeBitmap(Bitmap bmp)
- {
- //make output bmp.
- Bitmap b = new Bitmap(bmp.Width, bmp.Height);
- //get input bmp data for input and lock input bmp in memory
- BitmapData bmpD = bmp.LockBits(
- new Rectangle(0, 0, bmp.Width, bmp.Height),
- ImageLockMode.ReadOnly,
- bmp.PixelFormat);
- //get output bmp data for input and lock output bmp in memory
- BitmapData bD = b.LockBits(
- new Rectangle(0, 0, bmp.Width, bmp.Height),
- ImageLockMode.WriteOnly,
- bmp.PixelFormat);
- //make temp storage array.
- byte[] tempBytes = new byte[Math.Abs(bD.Stride) * b.Height];
- //get pointers to bitmap data.
- IntPtr ptrNew = bD.Scan0;
- IntPtr ptrOld = bmpD.Scan0;
- //copy input data to storage array.
- Marshal.Copy(ptrOld, tempBytes, 0, tempBytes.Length);
- //copy storage array to outputdata
- Marshal.Copy(tempBytes, 0, ptrNew, tempBytes.Length);
- //unlock bitmaps.
- b.UnlockBits(bD);
- bmp.UnlockBits(bmpD);
- //dispose input bitmap.
- bmp.Dispose();
- return b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement