mono / libgdiplus

C-based implementation of the GDI+ API
http://www.mono-project.com/
MIT License
329 stars 171 forks source link

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

Open jhergens opened 2 years ago

jhergens commented 2 years ago

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.