Transparency of picture box

后端 未结 3 1184
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 04:08

Im just looking for an answer in my simple problem. Here it is

I have a pricturebox that has image with transparent background i Set the picturebox

3条回答
  •  醉话见心
    2020-12-04 04:29

    Drawing the image using a graphics object is the recommended procedure if you're going to use it as a cursor. But if you sometime want to use a PictureBox (for reasons like being able to quickly change image using it's Image property, etc), that is possible too.

    This code will draw a better "transparent" background, by drawing each control behind your PictureBox on it's background.

    How to use:

    1) Create a custom class.

    2) Put Inherits PictureBox below the Public Class ... line.

    3) Paste this code inside the class:

    Protected Overrides Sub OnPaintBackground(e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaintBackground(e)
    
        If Parent IsNot Nothing Then
            Dim index As Integer = Parent.Controls.GetChildIndex(Me)
    
            For i As Integer = Parent.Controls.Count - 1 To index + 1 Step -1
                Dim c As Control = Parent.Controls(i)
                If c.Bounds.IntersectsWith(Bounds) AndAlso c.Visible = True Then
                    Dim bmp As New Bitmap(c.Width, c.Height, e.Graphics)
                    c.DrawToBitmap(bmp, c.ClientRectangle)
                    e.Graphics.TranslateTransform(c.Left - Left, c.Top - Top)
                    e.Graphics.DrawImageUnscaled(bmp, Point.Empty)
                    e.Graphics.TranslateTransform(Left - c.Left, Top - c.Top)
                    bmp.Dispose()
                End If
            Next
        End If
    End Sub
    

    4) Build your project.

    5) Select your class from the toolbox and add it to your form/usercontrol.

提交回复
热议问题