我需要处理和保存图像的原始bpp(1 - BnW,8 - 灰色,24色)。 处理后,我总是有24bpp(由于与Aforge.Net处理)。我通过原BPP节约功能,我使用下面的代码保存:C#为什么Bitmap.Save忽略位图的PixelFormat?
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有24 bpp的。
我需要保持原始图像的bpp和图像格式。
为什么Bitmap.Save忽略位图的PixelFormat?
============================================== =========
谢谢你们,我已经找到了解决办法: http://freeimage.sourceforge.net/license.html
有了这个免费的图书馆,可以保存图像(JPEG特)到灰阶8位的帮助。
那么,什么是问题?为什么`Bitmap.Save`忽略正在保存的图像的`PixelFormat`?或者我该如何解决带有索引像素格式的`Graphics.FromImage`引发的异常? – 2011-01-13 12:13:18
对不起,我还没有添加任何问题。问题是:“为什么Bitmap.Save忽略位图的PixelFormat?” – ieaglle 2011-01-13 12:17:03