C#转换32bpp的图像8bpp [英] C# Converting 32bpp image to 8bpp

查看:1033
本文介绍了C#转换32bpp的图像8bpp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个32bpp的截屏图像转换为使用C#的8bpp(或4bpp,或1bpp)格式。我已经看过类似的主题数计算器的答案,大部分建议使用下面的code的变化:

I'm trying to convert a 32bpp screenshot image to an 8bpp (or 4bpp, or 1bpp) format using C#. I've already looked at several stackoverflow answers on similar subjects and most suggest variations using the following code:

public static Bitmap Convert(Bitmap oldbmp) 
{
    Bitmap newbmp = new Bitmap(oldbmp.Width, oldbmp.Height, PixelFormat.Format8bppIndexed);

    Graphics gr = Graphics.FromImage(newbmp);

    gr.PageUnit = GraphicsUnit.Pixel;
    gr.DrawImageUnscaled(oldbmp, 0, 0);

    return newbmp;
}

不过,这在执行时,我得到一个异常: A的图形对象不能从具有索引像素格式图像创建。我的理解是8,4和1bpp图片是否有色差表映射,而不是实际的颜色像素本身(如32或16bpp的图像),所以我想我错过了一些转换步骤的地方,但我相当新的C#(即将从C ++背景),并将preFER才能够做到这一点使用本机C#调用,而不是诉诸PInvoking 的BitBlt 的GetDIBits 等任何人能够帮我解决这个?谢谢你。

However, when this executes, I get a the exception: A graphics object cannot be created from an image that has an indexed pixel format. I understand that 8, 4 and 1bpp images have colour table mappings rather than the actual colour pixels themselves (as in 32 or 16bpp images) so I assume I'm missing some conversion step somewhere, but I'm fairly new to C# (coming from a C++ background) and would prefer to be able do this using native C# calls rather than resorting to PInvoking BitBlt and GetDIBits etc. Anybody able to help me solve this? Thanks.

修改:我要指出的是,我需要这是向后兼容.NET框架2.0

EDIT: I should point out that I need this to be backwardly compatible to .NET framework 2.0

推荐答案

GDI +中一般具有索引像素格式很差的支持。有将图像转换用65536或1600万色到一个只具有2,16或256颜色必须从源图像中删除,这是一个有损耗的转换,可以有非常差的结果没有简单的方法。有可用来完成这个多种算法,他们都不是完美的,每一种形象。这是一个图形编辑器的工作。

GDI+ in general has very poor support for indexed pixel formats. There is no simple way to convert an image with 65536 or 16 million colors into one that only has 2, 16 or 256. Colors have to be removed from the source image and that is a lossy conversion that can have very poor results. There are multiple algorithms available to accomplish this, none of them are perfect for every kind of image. This is a job for a graphics editor.

还有一个绝招,我发现。 GDI +具有图像连接codeR为GIF文件。这是一个图形格式,只有256种颜色,带连接codeR的必须的限制的颜色数量。它采用了抖动算法这是适合照片。它有一个诀窍产生的网格图案,你会小于高兴当它。使用这样的:

There is one trick I found. GDI+ has an image encoder for GIF files. That's a graphics format that has only 256 colors, the encoder must limit the number of colors. It uses a dithering algorithm that's suitable for photos. It does have a knack for generating a grid pattern, you'll be less than thrilled when it does. Use it like this:

    public static Image Convert(Bitmap oldbmp) {
        using (var ms = new MemoryStream()) {
            oldbmp.Save(ms, ImageFormat.Gif);
            ms.Position = 0;
            return Image.FromStream(ms);
        }
    }

返回的图像与由EN codeR计算的面板条目8bpp像素格式。你可以施放它,如果必要的位图。到目前为止做的最好的事情是根本不与索引格式的麻烦。他们追溯到石器时代计算回来时记忆受到严重制约。或者使用一个专业的图形编辑器。

The returned image has a 8bpp pixel format with the Palette entries calculated by the encoder. You can cast it to Bitmap if necessary. By far the best thing to do is to simply not bother with indexed formats. They date from the stone age of computing back when memory was severely constrained. Or use a professional graphics editor.

这篇关于C#转换32bpp的图像8bpp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆