I was looking for the fastest way to convert Bitmap to 8bpp. I found 2 ways:
1.
public static System.Drawing.Image ConvertTo8bpp(Bitmap oldbmp) { using (var ms = new MemoryStream()) { oldbmp.Save(ms, ImageFormat.Gif); ms.Position = 0; return System.Drawing.Image.FromStream(ms); } }
2. http://www.wischik.com/lu/programmer/1bpp.html
But: 1. Results with very low quality (poor pallet)
and 2 gives me a bitmap with a negative step when I try to block and copy data to an array of bytes. I get an exception: Attempted to read or write protected memory. This often indicates that another memory is corrupted.
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat); this.stride = bmpData.Stride; this.bytesPerPixel = GetBytesPerPixel(bmp.PixelFormat); int length = bmpData.Stride * bmp.Height; if (this.stride < 0) this.data = new byte[-length]; else this.data = new byte[length]; Marshal.Copy(bmpData.Scan0, data, 0, length);
How can I do 2 gives a positive step? Or how can I copy data using negative step locks?
source share