.DrawImage有不透明度?

Vol*_*ort 3 vb.net visual-studio winforms

g.DrawImage
Run Code Online (Sandbox Code Playgroud)

要...是的,在我的图片框中画一个图像.是否可以赋予它不透明度属性?我一直在看DrawImage的其他版本但是找不到这样的东西!

Han*_*ant 7

您必须使用ColorMatrix来混合图像.这是我刚才写的C#控件,它向您展示了您需要的基本代码.不是VB.NET代码,但是,嘿,你没有尝试过真正的努力:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

public class BlendPanel : Panel {
    public BlendPanel() {
        DoubleBuffered = true;
    }
    public Image Image1 {
        get { return mImg1; }
        set { mImg1 = value; Invalidate(); }
    }
    public Image Image2 {
        get { return mImg2; }
        set { mImg2 = value; Invalidate(); }
    }
    public float Blend {
        get { return mBlend; }
        set { mBlend = value; Invalidate(); }
    }
    protected override void OnPaint(PaintEventArgs e) {
        if (mImg1 == null || mImg2 == null)
            e.Graphics.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(0, 0, this.Width, this.Height));
        else {
            Rectangle rc = new Rectangle(0, 0, this.Width, this.Height);
            ColorMatrix cm = new ColorMatrix();
            ImageAttributes ia = new ImageAttributes();
            cm.Matrix33 = mBlend;
            ia.SetColorMatrix(cm);
            e.Graphics.DrawImage(mImg2, rc, 0, 0, mImg2.Width, mImg2.Height, GraphicsUnit.Pixel, ia);
            cm.Matrix33 = 1F - mBlend;
            ia.SetColorMatrix(cm);
            e.Graphics.DrawImage(mImg1, rc, 0, 0, mImg1.Width, mImg1.Height, GraphicsUnit.Pixel, ia);
        }
        base.OnPaint(e);
    }
    private Image mImg1;
    private Image mImg2;
    private float mBlend;
}
Run Code Online (Sandbox Code Playgroud)