.DrawImage with opacity?

前端 未结 2 659
自闭症患者
自闭症患者 2020-11-29 12:48
g.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

相关标签:
2条回答
  • 2020-11-29 13:22

    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;
    }
    
    0 讨论(0)
  • 2020-11-29 13:29

    Thanks Hans Passant It is very useful! It saved my time

    I've converted you code into VB

    Imports System.Drawing
    Imports System.Drawing.Imaging
    Imports System.Windows.Forms
    
    Public Class BlendPanel
        Inherits Panel
        Public Sub New()
            DoubleBuffered = True
        End Sub
        Public Property Image1() As Image
            Get
                Return mImg1
            End Get
            Set
                mImg1 = value
                Invalidate()
            End Set
        End Property
        Public Property Image2() As Image
            Get
                Return mImg2
            End Get
            Set
                mImg2 = value
                Invalidate()
            End Set
        End Property
        Public Property Blend() As Single
            Get
                Return mBlend
            End Get
            Set
                mBlend = value
                Invalidate()
            End Set
        End Property
        Protected Overrides Sub OnPaint(e As PaintEventArgs)
            If mImg1 Is Nothing OrElse mImg2 Is Nothing Then
                e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), New Rectangle(0, 0, Me.Width, Me.Height))
            Else
                Dim rc As New Rectangle(0, 0, Me.Width, Me.Height)
                Dim cm As New ColorMatrix()
                Dim ia As 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)
            End If
            MyBase.OnPaint(e)
        End Sub
        Private mImg1 As Image
        Private mImg2 As Image
        Private mBlend As Single
    End Class
    
    0 讨论(0)
提交回复
热议问题