2

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:

Scanned Canon EOS User guide

As you can see this is not the expected image. I expect this as the result:

enter image description here

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?

Tobias Moe Thorstensen
  • 8,861
  • 16
  • 75
  • 143
  • This looks similar - http://stackoverflow.com/questions/6782489/create-bitmap-from-a-byte-array-of-pixel-data – Daniel Kelley Jun 03 '14 at 10:56
  • The expected output looks twice as broad as your result, e.g. at the logo. Where do you get the width+height values from? And have you tried `Format4bppIndexed`? Is the scanner output stored in a file? For raw pixel data you need to know its format. Or could it be the data are formatted and `Undefined`or `DontCare`would do?? (Just guessing) – TaW Jun 03 '14 at 11:03
  • @TaW I've tried with the DontCare and Undefined, but it does not work. The width and height values are stored in an object where this is method is called. Just to mention, the DPI is a fixed size of 300. – Tobias Moe Thorstensen Jun 03 '14 at 11:06
  • Just checking, but are you sure the data in the `data` array gets filled/processed correctly? It's _easy_ to "offset" an image by changing the parameters inside the `for` loop. I'm assuming you iterate over one dimension (usually the height) and access each color component of a pixel by using offsets. The image looks too "mathematically deformed" to be a "save to disk" related problem. – Andrei V Jun 03 '14 at 11:20
  • DPI is probably without relevance. I would look into those byte values with a hex editor and try to determine their format. Once more: where do the width+height values __originate__? Is it something you tell the scanner? The logo is ca 133px across but in your result about 200px. That sounds like you interpret 2byte for 3 , like using a 24bit format instead of 16bit. but that's not what you told us.. What is the total size? the large yellow stripe is also strange; nothing looks like a color table either. keep us posted, please – TaW Jun 03 '14 at 12:25

1 Answers1

0

After thinking about it, are you sure that PixelFormat.Format8bppIndexed is a correct format? The Bitmap is created and it - more or less - looks like the desired image. That shows that you give too much or too little data for a single picture - which means that your PixelFormat may be wrong.

You can try to experiment with different types and check which one works. They wary, dependent on how many color does your image has (the more colors there are, the bigger bites-per-pixel ratio you need).

Kamil T
  • 2,232
  • 1
  • 19
  • 26