Preventing Picture Clearing in VB.Net Program on PictureBox

  • Thread starter Thread starter JasonRox
  • Start date Start date
  • Tags Tags
    Picture
Click For Summary

Discussion Overview

The discussion revolves around preventing the clearing of images and text in a PictureBox control in a VB.Net program when the PictureBox is moved or redrawn. Participants explore various methods to maintain the visual elements displayed on the PictureBox during user interactions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • Some participants suggest using the Paint event of the PictureBox to redraw text and images, ensuring they persist during redraws.
  • Others propose drawing to an in-memory bitmap and setting the PictureBox's image to this bitmap during the Paint event.
  • One participant mentions the inefficiency of redrawing the memory bitmap every time the Paint event is called, suggesting that it should only be redrawn if the image is dynamic.
  • There are concerns about the complexity of implementing these methods, with some participants expressing uncertainty about how to handle errors encountered in their attempts.
  • A participant shares a specific code example for handling mouse clicks to draw text on the PictureBox, indicating that the drawing occurs only on user interaction.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to prevent the clearing of the PictureBox content. Multiple competing views and methods are presented, with some participants expressing uncertainty about their implementations.

Contextual Notes

Some participants mention encountering errors when trying to implement suggested methods, indicating potential limitations in their understanding or the complexity of the solutions discussed.

JasonRox
Homework Helper
Gold Member
Messages
2,394
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 the 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!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
11K
  • · Replies 6 ·
Replies
6
Views
9K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 266 ·
9
Replies
266
Views
32K
  • · Replies 1 ·
Replies
1
Views
10K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 42 ·
2
Replies
42
Views
7K