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)