2

I am trying to understand why I have troubles creating a Bitmap from a byte array. I post this after a careful scrutiny of the existing posts about Bitmap creation from byte arrays, like the followings:

My code is aimed to execute a filter on a digital image 8bppIndexed writing the pixel value on a byte[] buffer to be converted again (after some processing to manage gray levels) in a 8BppIndexed Bitmap

My input image is a trivial image created by means of specific perl code:

input image

Of course, after executing the filter the output image has lost the first and last rows and the first and last columns, due to the way the filter manage borders, so from the original 256 x 256 image i get a 254 x 254 image.

Just to stay focused on the issue I have commented the code responsible for executing the filter so that the operation really performed is an obvious:

ComputedPixel = InputImage.GetPixel(myColumn, myRow).R;  

I know, i should use lock and unlock but I prefer one headache one by one. Anyway this code should be a sort of identity transform, and at last i use:

private unsafe void FillOutputImage()
{           
    OutputImage = new Bitmap (OutputImageCols, OutputImageRows, PixelFormat .Format8bppIndexed);
    ColorPalette ncp = OutputImage.Palette;
    for (int i = 0; i < 256; i++)
        ncp.Entries[i] = Color .FromArgb(255, i, i, i);
    OutputImage.Palette = ncp;

    Rectangle area  = new Rectangle(0, 0, OutputImageCols,  OutputImageRows);
    var data = OutputImage.LockBits(area, ImageLockMode.WriteOnly, OutputImage.PixelFormat);
    Marshal.Copy(byteBuffer, 0, data.Scan0, byteBuffer.Length);

    OutputImage.UnlockBits(data);         
}

The output image I get is the following:

output image

It is quite clear that I am losing a pixel per row, but i cannot understand why:

  1. I have carefully controlled all the parameters: OutputImageCols, OutputImageRows and the byte[] byteBuffer length and content even writing known values as way to test.
  2. The code is nearly identical to other code posted in StackOverflow and elsewhere.

Someone maybe could help to identify where the problem is?

Community
  • 1
  • 1
Daniel
  • 1,357
  • 2
  • 19
  • 39
  • 2
    You are ignoring BitmapData.Stride. You'll get this effect if the number of bytes in a scanline is not divisible by 4. – Hans Passant Sep 12 '12 at 15:41
  • 1
    How should I manage the stride? In writing the values in the byte array so that rows are aligned on four bytes boundaries? – Daniel Sep 12 '12 at 15:49

1 Answers1

0

The answer is not properly mine, the true answer comes from Hans Passant that gave me the hint triggering the solution (thanks for that, Hans). I close my answer just providing a simple detail, just in case someone is interested: instead of working on a buffer and then copying it by means of the Marshal.Copy, dealing with the 4-byte alignement issue on a different buffer and using more memory than the strictly required one, I simply used LockBits to work directly on the image data, using a data structure already setup for the operation. This was the alternative I was thinking of in my question edited to Hans; I am aware that my question unfortunately has been not clearly stated. Anyway, now all works fine.

Community
  • 1
  • 1
Daniel
  • 1,357
  • 2
  • 19
  • 39