I have a file with raw image data. Every pixel is represented as 16 bit (0-65536)
value.
I'm reading the needed part of data into an ushort[]
array. After doing some image processing I need to show this image to the user somehow. I have no problem drawing an 8 bit grayscale (I just set every pixel's R, G and B values same), but can't handle 16 bit/pixel grayscale image.
Another possibilty is to use PixelFormat.Format48bppRgb
and set r,g and b to the same 16 bit grascale, but I couldn't find how.
I've found some code snippets using unsafe
code and LockBits
, but couldn't get them to work.
Is there any way to draw the 16 bit grayscale image without to downscale it to 8 bit?
Please provide me with a code snippet.
EDIT: this is how I set 8 bit grascales:
Array img;
//var data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, bmp.PixelFormat);
if (bitsPerElement == 8)
{
//img = Array.CreateInstance(typeof(byte), bitmapHeigth*bitmapWidth);
img = GetLayerBytes<byte>(LayerNum) as byte[];
}
else
{
img = GetLayerBytes<UInt16>(LayerNum) as UInt16[];
}
int ind = 0;
for (int heigth = 0; heigth < bitmapHeigth; heigth++)
{
for (int width = 0; width < bitmapWidth; width++)
{
int current = Convert.ToInt32(img.GetValue(ind));
ind++;
bmp.SetPixel(width, heigth, Color.FromArgb(current, current, current));
}
}
pictureBox1.Image = bmp;