如何从System.Window.media.PixelFormats.Gray16创建位图

时间:2009-05-20 12:24:08

标签: c# system.drawing

我从原始像素数据中成功绘制了图像(仅8位图像)。 这是执行相同操作的代码。

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

现在众所周知,c#2.0 GDI +不支持PixelFormat.format16bppGrayscale。 我用Google搜索并获得3.0 / 3.5框架支持。 所以我安装了两个。 支持的类是System.windows.media.PixelFormats。 PixelFormats.Gray16

现在我的问题是如何通过传递此参数来创建位图并获取要显示的图像。

我在那里得到了一些BitmapSource类,但我在C#3.0中非常新。

请帮帮我。

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

查看Greyscale filters中的AForge.net。您可以找到来源here

编辑:

作为关于我链接的源的说明,它使用的是AForge.NET的“旧”版本,但概念是相同的。