.DrawImage 不透明? [英] .DrawImage with opacity?

查看:39
本文介绍了.DrawImage 不透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

g.DrawImage

为了...是的,在我的图片框中绘制图像.是否可以给它一个不透明度属性?一直在看其他版本的DrawImage,没找到!

To.. yeah, draw an image in my picturebox. Is it possible to give it an opacity attribute? I have been looking at the other versions of DrawImage but couldn't find such thing!

推荐答案

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

You have to use a ColorMatrix to blend images. Here's a C# control I wrote a while ago, it shows you the basic code you'll need. Not VB.NET code but, hey, you didn't try real hard either:

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

这篇关于.DrawImage 不透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆