2

问题 1。 我自己的相关问题

我在这里问了下一个问题

现在第二个问题是。

当我尝试从原始像素数据中打开 16 位(Monocrome)图像时,我收到错误消息。因为我在创建 Bitmap 时使用 PixelFormat.Format16bppGrayscale

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

所以用谷歌搜索并发现不支持 Format16bppGrayscale 所以我修改了我的代码,如下所示。

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

令人惊奇的是,我现在正在获取图像,因为我更改了 pixelFormat。但问题是我的单色(灰度)图像看起来有各种颜色。

我怎样才能得到原来的样子。我尝试了几种灰度方法但不成功请给我一些不安全的代码。谢谢,

4

2 回答 2

2

BobPowell 的 GDI+ FAQ 有一点关于灰度的内容。.NET 中的 16 bpp 不起作用。我必须以 32 bpp 的速度完成所有操作,并借助外部工具进行转换。幸运的是(?)我大部分时间都坚持使用 TIFF 图像。此外,这个线程可能会有所帮助。

于 2009-04-28T12:57:01.410 回答
-1

您需要将托盘更改为灰度托盘。8bppIndexed 的默认托盘是 256 色。您可以像这样更改它:

ColorPalette pal = myBitmap.Palette;

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

myBitmap.Palette = pal;
于 2009-07-02T10:32:13.773 回答