本文介绍了C#为什么Bitmap.Save忽略位图的PixelFormat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要处理和保存图像在原来的BPP(1 - 为BNW,8 - 灰色,24 - 颜色)。
处理我总是有24 bpp的(由于与Aforge.Net处理)。我通过原BPP节约功能,我使用的下一个code保存:

I need to process and save images in their original bpp (1 - for BnW, 8 - for gray, 24 - color).After processing I always have 24bpp (due to processing with Aforge.Net). I pass original bpp to saving function and I'm using the next code for saving:

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

我收到一个例外:图形对象不能创建索引像素格式的图像,在该行:

I'm getting an exception: "Graphics object can't create image in indexed pixel format" in the line:

Graphics g = Graphics.FromImage(retval);

我试了下code:

I've tried the next code:

Bitmap s = new Bitmap("gray.jpg");
        Bitmap NewPicture = s.Clone(new Rectangle(0, 0, s.Width, s.Height),                   PixelFormat.Format8bppIndexed);

在这个NewImage拥有的PixelFormat 8bppIndexed,但是当我节省NewImage:

After this "NewImage" has PixelFormat 8bppIndexed, but when I'm saving NewImage:

NewPicture.Save("hello.jpg", ImageFormat.Jpeg);

hello.jpg有24 bpp的。

hello.jpg has 24bpp.

我需要保持原始图像的BPP和图像格式。

I need to keep bpp and image format of original image.

为什么Bitmap.Save忽略位图的PixelFormat?

Why Bitmap.Save ignores PixelFormat of Bitmap?

=============================================== ========

=======================================================

谢谢你们,我已经找到了解决办法:

Thanks guys, I've found a solution:http://freeimage.sourceforge.net/license.html

有了这个免费库的帮助,可以将图像(特别是JPEG)保存到灰阶8位。

With help of this free library it is possible to save images (especially JPEG ) to Grayscale 8-bit.

推荐答案

下面是code,将位图转换为1bpp / 8bpp位图。

Here is the code that will convert a Bitmap to 1bpp/8bpp bitmap. 1bpp/8bpp in C#.

在您的changePixelFormat方法,你可以检查什么fixel格式需要转换为,那么如果是1bpp或8 BPP然后用code的链接,并为所有其他格式使用code

In your changePixelFormat method you can check what fixel format it needs to be converted to, then if it's 1bpp or 8 bpp then use code from link and for all other formats use your code.

Graphics.FromImage 的有备注:

如果图像具有索引像素
  格式,此方法将引发
  与消息外,A
  Graphics对象不能从创建
  具有索引像素的图像
  格式。该索引的像素格式
  在下面的列表中显示。

Format1bppIndexed

Format4bppIndexed

Format8bppIndexed

新零件:

这是我发现的短谷歌上搜索后:

This is what i found after short googling:

8bpp灰度图像和8bpp JPG不与gdiplus版本支持
1.0。有gdiplus了Windows 7的新版本支持灰阶
图像和更增强codeC的支持。

8bpp greyscale images and 8bpp jpg is not supported with gdiplus version1.0. There is a new version of gdiplus in Windows 7 that supports greyscaleimages and more enhanced codec support.

8bpp索引JPEG图像与支持
微软的WIC API。

8bpp indexed jpeg images are supported withMicrosoft's WIC API.

您可以从微软的下载WIC探险家自己尝试一下
下载站点。

You can try it yourself by downloading the WIC explorer from Microsoft'sdownload site.

标准JPEG只支持24位图像。

Standard JPEG only supports 24-bit image.

这篇关于C#为什么Bitmap.Save忽略位图的PixelFormat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:52