Reputation: 3223
Is this the best way to paint images on a form? I have most of my experience in Java and little in VB.net. In Java there is repaint() that can be called? There is a Me.refresh(). Is this what you wnat to call when you have something to add to the form or to paint to the form.
Class:
Public Class Form1
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Create image.
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(50, 50)
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)
Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner2 As New Point(150, 150)
' Draw image to screen.
e.Graphics.DrawImage(newImage2, ulCorner2)
End Sub
End Class
I tried this code here with one button on the form with a click event
Public Class Form1
Dim i As Integer
Private Sub Form1_Paint(sender As System.Object, e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
i = 0
' Create image.
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner As New Point(50, 50)
' Draw image to screen.
e.Graphics.DrawImage(newImage, ulCorner)
Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner2 As New Point(150, 150)
' Draw image to screen.
e.Graphics.DrawImage(newImage2, ulCorner2)
If i = 5 Then
' Create image.
Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
' Create Point for upper-left corner of image.
Dim ulCorner3 As New Point(250, 250)
' Draw image to screen.
e.Graphics.DrawImage(newImage3, ulCorner3)
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 5
Me.Refresh()
End Sub
End Class
Just Tried this code here:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 5
Me.Invalidate()
End Sub
New Class just Tested:
Public Class Form2
Dim newImage As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Dim newImage2 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Dim newImage3 As Image = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Private bgImage As Bitmap
Private srcImages As Image()
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim img As Bitmap = Me.bgImage
If (img Is Nothing) Then
Me.bgImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
img = Me.bgImage
End If
If (Not Me.srcImages Is Nothing) Then
End If
MyBase.OnPaint(e)
End Sub
Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.srcImages = New Image(3 - 1) {}
Me.srcImages(0) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Me.srcImages(1) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
Me.srcImages(2) = Image.FromFile("C:\Users\itpr13266\Desktop\Pic\article-tip.gif")
End Sub
Private Sub _Disposed(sender As Object, e As EventArgs) Handles MyBase.Disposed
If (Not Me.srcImages Is Nothing) Then
For Each img As Image In Me.srcImages
If (Not img Is Nothing) Then
img.Dispose()
End If
Next
Me.srcImages = Nothing
End If
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private Sub _Resize(sender As Object, e As EventArgs) Handles MyBase.SizeChanged, MyBase.Resize
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.Invalidate()
End Sub
End Class
Upvotes: 2
Views: 9362
Reputation: 10001
Calling Me.Invalidate()
invalidates the entire surface of the control and causes the control to be redrawn.
I also suggest that you don't load your images in every paint cycle. You could load the source images in the forms load event and destroy them when the form disposes.
Public Class Form1
Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
Dim img As Bitmap = Me.bgImage
If (img Is Nothing) Then
Me.bgImage = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height)
img = Me.bgImage
End If
'TODO: Draw and paint.
If (Not Me.srcImages Is Nothing) Then
End If
MyBase.OnPaint(e)
End Sub
Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.srcImages = New Image(3 - 1) {}
Me.srcImages(0) = Image.FromFile("...")
Me.srcImages(1) = Image.FromFile("...")
Me.srcImages(2) = Image.FromFile("...")
End Sub
Private Sub _Disposed(sender As Object, e As EventArgs) Handles MyBase.Disposed
If (Not Me.srcImages Is Nothing) Then
For Each img As Image In Me.srcImages
If (Not img Is Nothing) Then
img.Dispose()
End If
Next
Me.srcImages = Nothing
End If
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private Sub _Resize(sender As Object, e As EventArgs) Handles MyBase.SizeChanged, MyBase.Resize
If (Not Me.bgImage Is Nothing) Then
Me.bgImage.Dispose()
Me.bgImage = Nothing
End If
End Sub
Private bgImage As Bitmap
Private srcImages As Image()
End Class
Upvotes: 2
Reputation: 942358
The 1st snippet shows no evidence of needing any help. Nothing in the Paint event handler depends on the state of class so there's no reason to force a repaint. Default painting is already good enough.
The 2nd snippet does, through the artificial i
variable. You'd always call Me.Invalidate()
in such a case, that marks the entire client region of the form as requiring a repaint. The UI then eventually gets a Paint event when nothing more important needs to be done. Using Me.Refresh()
works too but is heavy-handed and it is exceedingly rare to need it. I can't think of a good example.
Note how Invalidate() has some overloads, you can mark just a portion of the window to require repainting. This is an optimization that makes painting more efficient.
As written, your program is likely to crash after using it for a while. You must call the Dispose() method of the images you load. Take a look at the VB.NET Using statement. You'll want to pre-load the images so it doesn't happen while painting and won't happen repeatedly. Following a Winforms programming tutorial or book is highly recommended to avoid these kind of traps.
Upvotes: 4