I have a scanner that delivers Grayscale 8 bit images. My goal is to convert this image to Monochrome, which I already have implemented. To convert i need to operate on bitmap objects, rather than just the byte array. My code to get the Bitmap from the byte array looks like this:
public static Bitmap ByteArrayToBitmap(byte[] data, int width, int height)
{
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(data, 0, bmpData.Scan0, data.Length);
bmp.UnlockBits(bmpData);
bmp.Save(@"C:\Type7Test\TestImage.bmp", ImageFormat.Bmp);
return bmp;
}
My problem is that from this point the bitmap is getting formatted the wrong way. Notice the line where I save the bitmap to disk(just for testing), this is the result:
As you can see this is not the expected image. I expect this as the result:
I suspect that there is something wrong with the PixelFormats
and the creation of the bitmap. So can anyone pin-point me in the right direction to create a proper Bitmap from an array of raw image data?