Ricardo-007
07-20-2004, 06:21 PM
Hi, I want to draw permanent graphics ("AutoRedraw" in VB 6, "Do-it-yourself" in VB.NET) on a PictureBox without putting graphics methods on the Paint event (because its inefficiency). I've read that this can be made by drawing not directly on the PictureBox graphics object but in the PictureBox image graphics object.
I made this function based on a function that I found in a forum:
Function CreatePermanentGraphics(ByVal cajaImagen As PictureBox) As Graphics
Dim BMPImage As New Bitmap(cajaImagen.Width, cajaImagen.Height)
If cajaImagen.Image Is Nothing Then
cajaImagen.Image = BMPImage
Else
Graphics.FromImage(BMPImage).DrawImage(cajaImagen.Image, 0, 0)
cajaImagen.Image = BMPImage
End If
Return Graphics.FromImage(BMPImage)
End Function
Quick explanation: First time, if the PictureBox doesn't have an Image yet, I give it a new one, else, the new image will be the one it has previously. Then, the function returns the "BMPImage" graphics object.
Ok, I have 2 buttons and a PictureBox called pct1:
Private G As Graphics
Private Sub cmdCreatePermanentGraphics_Click(...) Handles cmdCreatePermanentGraphics.Click
G = CreatePermanentGraphics(pct1)
End Sub
Private Sub cmdDraw_Click(...) Handles cmdDraw.Click
G.DrawLine(Pens.Chocolate, 0, 0, 50, 50)
G.DrawEllipse(Pens.Chocolate, 0, 0, 50, 50)
End Sub
This procedure works just OK:
Sub CrearYDibujar()
cmdCreatePermanentGraphics.PerformClick()
cmdDraw.PerformClick()
End Sub
It shows immediately the shapes drawn. BUT, if the user click those buttons in that order, nothing happens, and it is necessary to refresh explicitly the control in order to view the results of the drawing. WHY???? they are the SAME procedures being executed in the SAME order. The core of my program is the drawing speed, and I just cannot refresh the control each time I draw something.
I would really appreciate you help. Please, help! (yes, you!, the one who is sitted there, HELP ME!!!, please!)
I made this function based on a function that I found in a forum:
Function CreatePermanentGraphics(ByVal cajaImagen As PictureBox) As Graphics
Dim BMPImage As New Bitmap(cajaImagen.Width, cajaImagen.Height)
If cajaImagen.Image Is Nothing Then
cajaImagen.Image = BMPImage
Else
Graphics.FromImage(BMPImage).DrawImage(cajaImagen.Image, 0, 0)
cajaImagen.Image = BMPImage
End If
Return Graphics.FromImage(BMPImage)
End Function
Quick explanation: First time, if the PictureBox doesn't have an Image yet, I give it a new one, else, the new image will be the one it has previously. Then, the function returns the "BMPImage" graphics object.
Ok, I have 2 buttons and a PictureBox called pct1:
Private G As Graphics
Private Sub cmdCreatePermanentGraphics_Click(...) Handles cmdCreatePermanentGraphics.Click
G = CreatePermanentGraphics(pct1)
End Sub
Private Sub cmdDraw_Click(...) Handles cmdDraw.Click
G.DrawLine(Pens.Chocolate, 0, 0, 50, 50)
G.DrawEllipse(Pens.Chocolate, 0, 0, 50, 50)
End Sub
This procedure works just OK:
Sub CrearYDibujar()
cmdCreatePermanentGraphics.PerformClick()
cmdDraw.PerformClick()
End Sub
It shows immediately the shapes drawn. BUT, if the user click those buttons in that order, nothing happens, and it is necessary to refresh explicitly the control in order to view the results of the drawing. WHY???? they are the SAME procedures being executed in the SAME order. The core of my program is the drawing speed, and I just cannot refresh the control each time I draw something.
I would really appreciate you help. Please, help! (yes, you!, the one who is sitted there, HELP ME!!!, please!)