Why is System.Drawing.Graphics empty RGB channels when Alpha == 0?

This has become a serious blocker for the program I am working on for processing images with alpha channels. Many of the images I have contain color information when the alpha channel is fully transparent and yet as soon as I try to load them into System.Drawing.Graphics it changes anything from Alpha 0 to Black from alpha 0.

Here's a basic example of the problem. I've looked around trying to find a cause, an answer, or a workaround, but I haven't found anything that even hints at this problem. 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");

      

EDIT:

After further testing, I don't really see a path using System.Drawing.Graphics, so my only solution, which is not really the answer, is to exclude System.Drawing.Graphics entirely. By looking at my code, it looks like I can avoid it. Its just after using System.Drawing.Graphics for drawing shapes, overlaying text on images, I find it annoying System.Drawing.Graphics to have a major drawback like this.

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

+3


source to share


1 answer


I think Vincent Povikk answered my question appropriately: Drawing PixelFormat32bppPARGB images with GDI + uses the regular formula instead of the pre-multiplied one

"The format of your foreground image doesn't matter (given that it has alpha), because you set it to Gdiplus :: Color. Color values ​​are defined as non-pre-multiplex, so gdiplus multiplies the components by the alpha value when it clears the foreground image The alternative would be to value the color to a different value depending on the format of the render target, and that’s the madness. ”



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

So, I do it myself.

0


source







All Articles