如何使System.Drawing.Image半透明?

Jad*_*ias 10 .net graphics transparency alpha image

System.Drawing.Graphics.DrawImage在另一个上粘贴一个图像.但我找不到透明度选项.

我已经在图像中绘制了我想要的所有内容,我只想让它变成半透明(alpha-transparency)

Jac*_*tti 14

没有"透明度"选项,因为您尝试执行的操作称为Alpha Blending.

public static class BitmapExtensions
{
    public static Image SetOpacity(this Image image, float opacity)
    {
        var colorMatrix = new ColorMatrix();
        colorMatrix.Matrix33 = opacity;
        var imageAttributes = new ImageAttributes();
        imageAttributes.SetColorMatrix(
            colorMatrix,
            ColorMatrixFlag.Default,
            ColorAdjustType.Bitmap);
        var output = new Bitmap(image.Width, image.Height);
        using (var gfx = Graphics.FromImage(output))
        {
            gfx.SmoothingMode = SmoothingMode.AntiAlias;
            gfx.DrawImage(
                image,
                new Rectangle(0, 0, image.Width, image.Height),
                0,
                0,
                image.Width,
                image.Height,
                GraphicsUnit.Pixel,
                imageAttributes);
        }
        return output;
    }
}
Run Code Online (Sandbox Code Playgroud)

Alpha混合