C# – Getting color image from bitmap while image is 16bppGrayscale

bitmapcimaging

Problem No1.
My own Related problem

I asked the next question here

Now the Problem No 2 is.

When i am trying to open 16 Bit (Monocrome ) images from their raw pixel data
then i am getting Error.
Because i am using PixelFormat.Format16bppGrayscale on creation of Bitmap like

Bitmap bmp = new Bitmap(Img_Width, Img_Height,PixelFormat.Format16bppGrayscale);

So googled and found Format16bppGrayscale not supported so i modifed my code like below.

 PixelFormat format = PixelFormat.Format16bppRgb565;
 Bitmap bmp = new Bitmap(Img_Width, Img_Height, format);
 Rectangle rect = new Rectangle(0, 0, Img_Width, Img_Height);
 BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, format);
 Marshal.Copy(rawPixel, 0, bmpData.Scan0, rawPixel.Length);
 bmp.UnlockBits(bmpData);

Amazing thing is that i am getting the image now because i change the pixelFormat.
But problem is that My monocrome (Grayscale) image look in various color.

How can i get the original appearance. I tried several grayscale method but not successful
Please give me some unsafe code.
Thanks,

Best Answer

BobPowell's GDI+ FAQ has a bit about greyscale. The 16 bpp in .NET just doesn't work. I have to do everything in 32 bpp and resort to external tools for conversion. Luckily (?) I get to stick with TIFF images most of the time. Also, this thread might help.