Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a PictureBox added to my Panel1 with Panel1.Controls.Add(pb), and I have tried to make my .png picture transparent.
I have tried with Color.Transparent and with System.Drawing.Color.Transparent, but when I add the PictureBox to my Panel, I can not make it transparent.

And also I'm not able to bring to the front of the others images.

This is my code.

Private Function molduraint()

    Dim pb As New PictureBox

    pb.BringToFront()
    pb.ImageLocation = OpenFileDialog1.FileName
    pb.SizeMode = PictureBoxSizeMode.StretchImage

    Panel1.Controls.Add(pb)

    pb.Location = New Point(txtValueX.Text, txtValueY.Text)
    If txtValueX.Text = 0 Or txtValueY.Text = 0 Then
        pb.Location = New Point(300, 172)
    End If

    pb.Visible = True
    pb.Size = New Size(TrackBar1.Value, TrackBar2.Value)
    pb.Image = PictureBox1.Image

End Function
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

As you probably know, WinForms controls are not exactly designed to support true transparency (except Forms, those can be actually transparent).

Bitmaps, on the other hand, support transparency.
If you create a Bitmap object using an image format that supports an Alpha Channel, like a .png bitmap, you can draw that image preserving its transparency.

The first thing to do is to create an object that can be used to reference each Bitmap we want to draw, so we can keep track of them.
Since you want to be able to specify position and size of these objects, those are two of the properties that the object must have. I'm adding some more that can be helpful here.

Public Class BitmapObject
    Public Property Name As String
    Public Property Image As Bitmap
    Public Property Position As Point
    Public Property Size As Size
    Public Property Order As Integer
End Class

The property Name will be the name of source file and Order will reference the z-order position of the Bitmap in relation to the other Bitmaps drawn inside a container.
All the Bitmaps will be grouped using a List of Bitmap objects, so we can summon them using the List Index or one of the properties.

Public MyBitmaps As List(Of BitmapObject) = New List(Of BitmapObject)

As for the drawing surface (canvas), we can use the Form itself, a PictureBox or a Panel (because they're - more or less - just surfaces). I prefer a Panel, it's lightweight, it can host other controls and can be moved around if needed.

If you want to draw on a control, you just need to subscribe its Paint() event and raise it calling the control's Invalidate() method.

Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
    If MyBitmaps.Count > 0 Then
        MyBitmaps.OrderBy(Function(item) item.Order).
            Select(Function(item)
                       e.Graphics.DrawImage(item.Image, New Rectangle(item.Position, item.Size))
                       Return item
                   End Function).ToList()
    End If
End Sub

To add a Bitmap to the List(Of BitmapObject), since you want to use an OpenFileDialog to let the user select a Bitmap, we assign this functionality to a Button and when the Bitmap is selected, a new BitmapObject is created and appended to the List.

Private Sub btnOpenFile_Click(sender As Object, e As EventArgs) Handles btnOpenFile.Click

    Dim fd As OpenFileDialog = New OpenFileDialog()
    fd.InitialDirectory = "[Images Path]"
    Dim dr As DialogResult = fd.ShowDialog()

    If dr = Windows.Forms.DialogResult.OK Then
        Dim BitmapName As String = New FileInfo(fd.FileName).Name

        Using tmpBitmap As Bitmap = New Bitmap(fd.FileName)
            MyBitmaps.Add(New BitmapObject With {
                          .Image = New Bitmap(tmpBitmap),
                          .Position = New Point(Integer.Parse(TextBox1.Text), Integer.Parse(TextBox2.Text)),
                          .Size = New Size(tmpBitmap.Height, tmpBitmap.Width),
                          .Order = MyBitmaps.Count,
                          .Name = BitmapName})

            ComboBox1.Items.Add(BitmapName)
            ComboBox1.SelectedIndex = MyBitmaps.Count - 1
            TrackBar1.Value = tmpBitmap.Height
            TrackBar2.Value = tmpBitmap.Width
            Panel1.Invalidate()
        End Using
    End If
End Sub

This is the result: (Full source code in PasteBin)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...