tags:

views:

262

answers:

2

Hi,

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,

+1  A: 

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

hometoast
I previously saw that post.I saw BobPowell faqs and tried that code.Images color are just changed to some random color but not in grayscale.I also saw that post from bytecode.He is asking same thing as me.I also working for medical images.
prashant
A: 

You need to change the pallet to a greyscale pallet. The default pallet for 8bppIndexed is 256 colour. You can change it like this:

ColorPalette pal = myBitmap.Palette;

for (int i = 0; i < pal.Entries.Length; i++)
    pal.Entries[i] = Color.FromArgb(i, i, i);

myBitmap.Palette = pal;
badbod99