Tutorial :How to create Bitmap from System.Window.media.PixelFormats.Gray16



Question:

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.


Solution:1

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;  }  


Solution:2

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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »