如何转换的32位RGBA图片为灰度preserving阿尔法 [英] How to convert 32-bit RGBA Image to Grayscale preserving alpha

查看:204
本文介绍了如何转换的32位RGBA图片为灰度preserving阿尔法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,在code和需求,将一个32位的RGBA图像对象(原来是32位PNG),其32位灰度对应。

I'd like to, in code and on demand, convert a 32-bit RGBA Image object (originally a 32-bit PNG) to its 32-bit grayscale counterpart.

我已经阅读几个<一href="http://stackoverflow.com/questions/1470143/convert-bitmapimage-to-grayscale-and-keep-alpha-channel">other这里的问题,还有很多网上的文章。我已经使用嘉洛斯做尝试,但它似乎不处理阿尔法非常好。像素是完全不透明的灰阶完美。任何像素是部分透明似乎并没有转化以及还有颜色的像素的淡色调。这足以显着。

I've already read several other questions here, as well as many articles online. I've tried using ColorMatrix to do it, but it doesn't seem to handle the alpha very well. Pixels that are entirely opaque grayscale perfectly. Any pixel that is partially transparent seems not to translate well as there are still tinges of color in those pixels. It is enough to be noticeable.

在我使用嘉洛斯如下:

new System.Drawing.Imaging.ColorMatrix(new float[][]{
                    new float[] {0.299f, 0.299f, 0.299f, 0, 0},
                    new float[] {0.587f, 0.587f, 0.587f, 0, 0},
                    new float[] {0.114f, 0.114f, 0.114f, 0, 0},
                    new float[] {     0,      0,      0, 1, 0},
                    new float[] {     0,      0,      0, 0, 1}
                    });

这是,因为我读过,一个pretty的标准NTSC加权矩阵。然后,我用它,以及 Graphics.DrawImage ,但正如我所说的部分透明的像素依然着色。我要指出,这是在白色背景上显示通过一个WinForms 图片框的Image对象。难道或许只是顺便图片框的画自己的形象和处理的透明部分?背景颜色不影响它(色彩的色彩是肯定的原始图像),但也许图片框是不正确重绘透明像素?

This is, as I've read, a pretty standard NTSC weighted matrix. I then use it, along with Graphics.DrawImage, but as I said the partially transparent pixels are still colored. I should point out this is displaying the Image object via a WinForms PictureBox on a white background. Could it be perhaps just the way PictureBox's draw their images and handle the transparent parts? The background colors are not affecting it (the tinge of color is from the original image for sure), but perhaps PictureBox isn't redrawing the transparent pixels correctly?

我已经看到了使用FormatConvertedBitmap以及一个OpacityMask一些方法。我还没有尝试过,主要是因为我真的preFER不要有进口presentationCore.dll(更不用提,这意味着它不会工作在.NET 2.0有限的应用程序)。当然,基本的System.Drawing中。*的东西可以做这个简单的程序?还是不行?

I've seen some methods that use a FormatConvertedBitmap along with an OpacityMask. I haven't tried it, mainly because I'd really prefer not to have to import PresentationCore.dll (not to mention that means it won't work in .NET 2.0 limited apps). Surely the basic System.Drawing.* stuff can do this simple procedure? Or not?

推荐答案

您任何机会绘画使用嘉洛斯图像到自身?当然,这是不行的(因为如果你画的东西半透明的灰色在一个绿色像素,一些绿色将彪炳)。你需要把它漆成到只含有透明像素的新的空位。

Are you by any chance painting the image onto itself using a ColorMatrix? That won't work of course (because if you paint something semi-transparent-gray over a green pixel, some green will shine through). You need to paint it onto a new, empty bitmap containing only transparent pixels.

这篇关于如何转换的32位RGBA图片为灰度preserving阿尔法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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