Как сделать Систему. Рисование. Полупрозрачное изображение?

System.Drawing.Graphics.DrawImage вставки одно изображение на другом. Но я не мог найти параметр прозрачности.

Я уже потянул все, что я хочу в изображении, я только хочу сделать его полупрозрачным (альфа-прозрачность)

10
задан Jader Dias 4 February 2010 в 15:59
поделиться

2 ответа

Опции "прозрачность" нет, потому что то, что вы пытаетесь сделать, называется Альфа Смешивание.

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;
    }
}

Alpha Blending

14
ответ дан 3 December 2019 в 22:37
поделиться

Я скопировал ответ по ссылке Митч, при условии, что я думаю, что он будет работать для меня:

public static Bitmap SetOpacity(this Bitmap bitmap, int alpha)
{
    var output = new Bitmap(bitmap.Width, bitmap.Height);
    foreach (var i in Enumerable.Range(0, output.Palette.Entries.Length))
    {
        var color = output.Palette.Entries[i];
        output.Palette.Entries[i] =
            Color.FromArgb(alpha, color.R, color.G, color.B);
    }
    BitmapData src = bitmap.LockBits(
        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
        ImageLockMode.ReadOnly,
        bitmap.PixelFormat);
    BitmapData dst = output.LockBits(
        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
        ImageLockMode.WriteOnly,
        output.PixelFormat);
    bitmap.UnlockBits(src);
    output.UnlockBits(dst);
    return output;
}
-1
ответ дан 3 December 2019 в 22:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: