本文介绍了为什么System.Drawing.Graphics空白的RGB通道时,阿尔法== 0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经成为一个程序我工作的操作有阿尔法通道的图像严重阻断。
我很多包含有颜色信息,其中一个alpha通道是完全透明的,而当我尝试将它们装入System.Drawing.Graphics尚未图像,它改变任何东西以0阿尔法,走进黑色带一个Alpha为0。

This has become a serious blocker for a program I'm working on to manipulate images that have Alpha channels.Many of the images I have contain color information where an Alpha channel is completely transparent, and yet as soon as I try to load them into System.Drawing.Graphics, it changes anything with of Alpha of 0, into Black with an Alpha of 0.

下面是问题的一个基本的样本。
我环顾四周,试图找到一个理由,答案或解决办法,但我没有发现任何东西,即使是暗示了这个问题。
任何的帮助,将在这一点赞赏。

Here is a basic sample of the issue.I have looked around trying to find a reason, answer, or workaround, but I haven't found anything that even alludes to this issue.Any help would be appreciated at this point.

var myTestTransparentColor = Color.FromArgb(0, 255, 128, 64);
var image = new Bitmap(135, 135, PixelFormat.Format32bppArgb);

using (var g = Graphics.FromImage(image))
{
    g.Clear(myTestTransparentColor);
}

var color = image.GetPixel(0, 0);

Debug.Assert(color == myTestTransparentColor, "channels must match original");



编辑:

在进一步的测试我真的不明白的方式提前使用System.Drawing.Graphics,所以我这是不是一个真正的答案唯一的解决办法,就是要完全避免System.Drawing.Graphics。通过我的代码看,它看起来像我能避免它。
它只是经过多年的使用System.Drawing.Graphics绘制图形,图像上种植的文字,我觉得很刺激的System.Drawing.Graphics有这样的缺点显著

After further testing I don't really see a way ahead by using System.Drawing.Graphics, so my only solution which is not really an answer, is to avoid System.Drawing.Graphics entirely. Looking through my code, it looks like I can avoid it.Its just after years of using System.Drawing.Graphics for drawing shapes, planting text over images, I find it irritating for System.Drawing.Graphics to have a significant drawback like this.

我还是想知道我是否可以使用System.Drawing.Graphics并保持我的ARGB完好,但我想我可以没有它活了。

I still would like to know if I can use System.Drawing.Graphics and keep my ARGB intact, but I guess I can live without it for now.

推荐答案

我觉得文森特Povirk已经回答了我的问题在这里适当:的之一的

I think Vincent Povirk has answered my question appropriately here: Drawing PixelFormat32bppPARGB images with GDI+ uses conventional formula instead of premultiplied one

你的前景图像的格式没有按T重要(因为它有阿尔法),因为你将它设置为一个Gdiplus ::颜色。颜色值定义为非预乘,所以gdiplus被清除时,前景图像的alpha值相乘的组件。另一种选择将是颜色值有不同的含义取决于渲染目标的格式,而出路在于疯狂。

"The format of your foreground image doesn't matter (given that it has alpha) because you're setting it to a Gdiplus::Color. Color values are defined as non-premultiplied, so gdiplus multiplies the components by the alpha value when it clears the foreground image. The alternative would be for Color values to have different meaning depending on the format of the render target, and that way lies madness."

如果你真的想这样级别的控制权渲染,你必须锁定位图位和自己做。

"If you really want this level of control over the rendering, you'll have to lock the bitmap bits and do it yourself."

所以,我做我自己。

这篇关于为什么System.Drawing.Graphics空白的RGB通道时,阿尔法== 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:00