Preventing Picture Clearing in VB.Net Program on PictureBox

  • Thread starter Thread starter JasonRox
  • Start date Start date
  • Tags Tags
    Picture
AI Thread Summary
The discussion revolves around a programming issue related to a Picture Box in a graphical user interface application. The main problem is that when the Picture Box is moved, the drawn images and text disappear, reverting to the default state. To resolve this, participants suggest using a paint event to redraw the content whenever the Picture Box is refreshed or moved. Specifically, it is recommended to draw to an in-memory bitmap and set the Picture Box's image to this bitmap during the paint event. This approach ensures that the text and images persist even after the Picture Box is moved. Additionally, there are suggestions to optimize performance by minimizing the frequency of redrawing operations, as drawing text can be resource-intensive compared to simpler image blitting techniques. The conversation concludes with a participant expressing intent to implement the suggested solution.
JasonRox
Homework Helper
Gold Member
Messages
2,381
Reaction score
4
Hey guys,

Working on a program.

My program mostly takes place on a Picture Box where a user can click the picture in different areas and different images and sentences and words come up.

Hence, I used the following often...

g = PictureBox1.CreateGraphics()
g.DrawString("Hello World", font, brush, 50, 50)

But the images clear if the box is moved over anything. Hence a user who's playing and then decides to send a message to his friend will have his Picture cleared. How can I keep this from happening?
 
Technology news on Phys.org
JasonRox said:
Hey guys,

Working on a program.

My program mostly takes place on a Picture Box where a user can click the picture in different areas and different images and sentences and words come up.

Hence, I used the following often...

g = PictureBox1.CreateGraphics()
g.DrawString("Hello World", font, brush, 50, 50)

But the images clear if the box is moved over anything. Hence a user who's playing and then decides to send a message to his friend will have his Picture cleared. How can I keep this from happening?

Are you redrawing the box when it gets moved?
 
DavidSnider said:
Are you redrawing the box when it gets moved?

Oh no, the stuff is already plotted.

THen you move it, and it gets erased and teh default picture is shown.
 
Try drawing to an in-memory bitmap and then on the OnDraw event of the picturebox set the picturebox bitmap to the memory bitmap.
 
You need to have a "paint event" for the picture box and draw the string there. That way any time the picture box redrawn your text will be redrawn also. Example:

Code:
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        e.Graphics.DrawString("Hello world", Font, brush, 50, 50)

End Sub
 
TurtleMeister said:
You need to have a "paint event" for the picture box and draw the string there. That way any time the picture box redrawn your text will be redrawn also. Example:

Code:
Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        e.Graphics.DrawString("Hello world", Font, brush, 50, 50)

End Sub

Well, then this draws it right away. Here is an idea of what I'm doing...

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

g.Drawstring("Hello World!", font, brush, 50, 50)

End Sub

So, the event only occurs if they click on it
 
DavidSnider said:
Try drawing to an in-memory bitmap and then on the OnDraw event of the picturebox set the picturebox bitmap to the memory bitmap.

I tried doing that before and my code always had errors.
 
You can use logic in the paint event.

Code:
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        PaintMyString = True

End Sub

Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        If PaintMyString = True Then
            e.Graphics.DrawString("Hello world", Font1, Brushes.Black, 20, 20)
        End If

End Sub
 
I don't think you want to redraw the memory bitmap every time Paint is called (unless your image is dynamic in some way). I'd draw it to memory once and then redraw the bitmap.

DrawString takes a lot of cycles compared to a plain ol' blit.
 
  • #10
DavidSnider said:
I don't think you want to redraw the memory bitmap every time Paint is called (unless your image is dynamic in some way). I'd draw it to memory once and then redraw the bitmap.

DrawString takes a lot of cycles compared to a plain ol' blit.

I have no idea how to do even that.
 
  • #11
I think this method will accomplish what DavidSnider is talking about. Put the code in your mousedown event.

Code:
g = Graphics.FromImage(PictureBox1.Image)
g.DrawString("Hello World", font, brush, 50, 50)
PictureBox1.Refresh()
 
  • #12
TurtleMeister said:
I think this method will accomplish what DavidSnider is talking about. Put the code in your mousedown event.

Code:
g = Graphics.FromImage(PictureBox1.Image)
g.DrawString("Hello World", font, brush, 50, 50)
PictureBox1.Refresh()

I'm going to try that out. Thanks!
 
Back
Top