libgdiplus icon indicating copy to clipboard operation
libgdiplus copied to clipboard

Graphics.DrawImage with ImageAttributes for transparency does not work with 24bpp bitmaps

Open jhergens opened this issue 4 years ago • 0 comments

Graphics.DrawImage with ImageAttributes for transparency does not work with 24bpp bitmaps. The alpha values are just multiplied into the color, and you end up with a dark image.

The below code:

var bitmap = new Bitmap(100, 100);
using (var graphics = Graphics.FromImage(bitmap))
{
    graphics.Clear(Color.White);
 
    var redBitmap = new Bitmap(50, 50, PixelFormat.Format24bppRgb);
    using (var redGraphics = Graphics.FromImage(redBitmap))
    {
        redGraphics.Clear(Color.Red);
    }
    
    var destRect = new RectangleF(25, 25, 50, 50);
    var srcRect = new RectangleF(0, 0, 50, 50);
 
    PointF[] destRectanglePoints =
    {
        new(destRect.Left, destRect.Top),
        new(destRect.Right, destRect.Top),
        new(destRect.Left, destRect.Bottom)
    };
 
    var attributes = new ImageAttributes();
    attributes.SetColorMatrix(new ColorMatrix { Matrix33 = 0.5f });
    graphics.DrawImage(redBitmap, destRectanglePoints, srcRect, GraphicsUnit.Pixel, attributes);
}

Renders like this: screenshot-1

It is expected to render like this: screenshot-2

It works as expected for 32 but pixel formats.

We are building libgdiplus from main.

jhergens avatar Dec 06 '21 17:12 jhergens