【发布时间】:2011-06-08 11:12:06
【问题描述】:
我需要以原始 bpp 处理和保存图像(1 - 用于 BnW,8 - 用于灰色,24 - 彩色)。 处理后我总是有 24bpp(由于使用 Aforge.Net 处理)。我将原始 bpp 传递给保存函数,我正在使用下一个代码进行保存:
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;
}
当我通过时:
PixelFormat.Format8bppIndexed
我遇到了一个异常:“图形对象无法以索引像素格式创建图像”:
Graphics g = Graphics.FromImage(retval);
我已经尝试了下一个代码:
Bitmap s = new Bitmap("gray.jpg");
Bitmap NewPicture = s.Clone(new Rectangle(0, 0, s.Width, s.Height), PixelFormat.Format8bppIndexed);
在此“NewImage”具有 PixelFormat 8bppIndexed 之后,但是当我保存 NewImage 时:
NewPicture.Save("hello.jpg", ImageFormat.Jpeg);
hello.jpg 有 24bpp。
我需要保留原始图像的bpp和图像格式。
为什么Bitmap.Save会忽略Bitmap的PixelFormat?
================================================ ========
谢谢各位,我找到了解决办法: http://freeimage.sourceforge.net/license.html
借助这个免费库,可以将图像(尤其是 JPEG)保存为灰度 8 位。
【问题讨论】:
-
那么,问题是什么?为什么
Bitmap.Save会忽略正在保存的图片的PixelFormat?或者我如何使用索引像素格式解决Graphics.FromImage引发的异常? -
抱歉,我没有添加问题。问题是:“为什么 Bitmap.Save 会忽略 Bitmap 的 PixelFormat?”
-
如果您想要 8 位索引,则必须将其保存为 GIF。
-
GDI+ 无法做到这一点,它对索引像素格式的支持非常弱。并没有什么意义,这在 Windows 3 天很重要。当操作系统与您今天使用的桌面壁纸图像一样大时。
-
@HansPassant 如果您使用非常大的位图(例如:naturalearthdata.com/downloads/10m-raster-data/10m-gray-earth),仍然可以使用它
标签: c# image-processing bitmap pixelformat