Public Class Form1
Dim pb As New PictureBox With
{.Parent = Me, .Top = 10, .Left = 10, .Width = 300, .Height = 300,
.SizeMode = PictureBoxSizeMode.Zoom, .BackColor = Color.White,
.BorderStyle = BorderStyle.Fixed3D}
Dim WithEvents btnCreateBitmap As New Button With
{.Parent = Me, .Top = 320, .Left = 10, .Width = 300, .Text = "Create"}
Dim WithEvents btnKillBitmap As New Button With
{.Parent = Me, .Top = 360, .Left = 10, .Width = 300, .Text = "Kill"}
Dim WithEvents btnSaveBitmap As New Button With
{.Parent = Me, .Top = 400, .Left = 10, .Width = 300, .Text = "Save"}
Dim Gleisplan As Bitmap
Dim rndm As New Random(Now.Millisecond)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.MinimumSize = New Size(350, 500)
End Sub
Private Sub CreateBitmap()
DeleteBitmap() 'Freigabe sicher stellen
Dim Gpx, GPy, x1, y1, x2, y2 As Integer
Gpx = 24361
GPy = 5966
Gleisplan = New Bitmap(Gpx, GPy,
Imaging.PixelFormat.Format32bppArgb)
pb.Image = Gleisplan
Using g As Graphics = Graphics.FromImage(Gleisplan),
pn As New System.Drawing.Pen(Color.Blue, 8)
g.Clear(Color.Yellow)
For i As Integer = 0 To 10
x2 = x1
y2 = y1
x1 = CInt(rndm.NextDouble() * Gpx)
y1 = CInt(rndm.NextDouble() * GPy)
g.DrawLine(pn, x1, y1, x2, y2)
Next i
End Using
End Sub
Private Sub SaveBitmap()
If Gleisplan Is Nothing Then Exit Sub
Dim filepath As String = "G:\downloads\gleisplan.png"
IO.File.Delete(filepath)
Gleisplan.Save(filepath, Drawing.Imaging.ImageFormat.Png)
End Sub
Private Sub DeleteBitmap()
'Referenz in der Picturebox aufheben
pb.Image = Nothing
If Gleisplan IsNot Nothing Then
'Windows-Resourcen (Handles etc.) der Bitmap entfernen
Gleisplan.Dispose
'Letzte Referenz auf die Bitmap aufheben
'(Löschen der Net-Klasse anfordern)
Gleisplan = Nothing
End If
End Sub
Private Sub btnKillBitmap_Click(sender As Object, e As EventArgs) Handles _
btnKillBitmap.Click
DeleteBitmap()
End Sub
Private Sub btnCreateBitmap_Click(sender As Object, e As EventArgs) Handles _
btnCreateBitmap.Click
CreateBitmap()
End Sub
Private Sub btnSaveBitmap_Click(sender As Object, e As EventArgs) Handles _
btnSaveBitmap.Click
SaveBitmap()
End Sub
End Class |