Writing a 16-bit grayscale image

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have a byte array containing pixel values for a 16bpp grayscale image.  I need a solution for converting said array to a bitmap image.  So far Ive come up with this:
 
internal static Image imageFromArray(byte[] array)
{
const int WIDTH = 1472;
int height = (array.Length) / WIDTH / 2;
Bitmap bitmap = new Bitmap(1472, height, PixelFormat.Format16bppGrayScale);
for (int x = 0; x < height; x++)
for (int y = 0; y < WIDTH; y++)
bitmap.SetPixel(x, y, (Color)array[(y * 1472 + x) * 2]); Image image = bitmap;
return image;
 }
But there is an error because I cant cast the byte as a color.  So my question is, how do I actually translate my pixel data from the byte[] to the bitmap?  Is there a better way to do this using an unsafe code block?
Thanks for your help.  If its relevant, the image files I am using are about 16MB.  They contain raw data, some number of rows by 1472 columns of unsigned 16-bit words.  They are all 1472 pixels wide.

View the full article
 
Back
Top