Fill bitmap with 16 bit grayscale values without d

2019-09-07 17:34发布

问题:

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;

回答1:

Quote from Hans Passant in case somebody is looking for the answer :

These pixel formats were defined in GDI+ with the expectation that someday it might be useful with sufficient advances in display technology. We are still waiting for that. It actually went the other way with LCD panels not being able to display all possible 16 million colors from a 24rgb format. Actually seeing the gray levels in a 16-bit format requires specialized hardware and a highly trained eye, the kind you find in a hospital. With a matching non-standard image format, like DICOM. You can buy this from a vendor like Lead Tools.