views:

347

answers:

2

Hi, I am successfully drawn images from their raw pixel data.(only 8 bit images). here is the code for doing the same thing.

     PixelFormat format = PixelFormat.Format8bppIndexed;
     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);

Now as you all know PixelFormat.format16bppGrayscale is not supported by c# 2.0 GDI+. I googled and got 3.0/3.5 framework support this. So i installed both. The class which is support is System.windows.media.PixelFormats. PixelFormats.Gray16

Now my problem is how to create a bitmap and get a image for display by passing this parameter.

i got something BitmapSource class there but i am very new in C#3.0.

Please help me.

A: 

Try this:

private static Bitmap changePixelFormat(Bitmap input, PixelFormat format)
{
    Bitmap retval=new Bitmap(input.Width, input.Height, format);
    retval.SetResolution(input.HorizontalResolution, input.VerticalResolution);
    Graphics g = Graphics.FromImage(retval);
    g.DrawImage(input, 0, 0);
    g.Dispose();
    return retval;
}
Brian
I admit this is not precisely what you are asking for, but it should either work as an alternative or be adjustable by creating a new input bitmap and copying the raw data onto it so that you'll have something to pass into changePixelFormat.
Brian
Thanks Brian for you effort.But it will not work for me.
prashant
A: 

Take a look at the Greyscale filters in AForge.net. You can find source here.

EDIT:

As a note on that source I linked, it is using an 'old' version of AForge.NET, but the concepts are the same.

JasonRShaver
thanks Jason,I tried that code earlier but image display is not proper.I will tell you it can work if you pass 16bppRGB555 but image looks in color.But you cannot pass 16bppGrayscale.so not correct display.
prashant