Preventing Picture Clearing in VB.Net Program on PictureBox

  • Thread starter JasonRox
  • Start date
  • Tags
    Picture
In summary, DavidSnider is talking about how to keep the user's image from clearing when he sends a message to a friend by drawing the string in an in-memory bitmap and then setting the picture box bitmap to the memory bitmap when it gets redrawn.
  • #1
JasonRox
Homework Helper
Gold Member
2,386
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
  • #2
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?
 
  • #3
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.
 
  • #4
Try drawing to an in-memory bitmap and then on the OnDraw event of the picturebox set the picturebox bitmap to the memory bitmap.
 
  • #5
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
 
  • #6
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
 
  • #7
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.
 
  • #8
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
 
  • #9
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!
 

1. What is VB.Net Picture Help?

VB.Net Picture Help is a collection of tools and resources designed to assist developers in working with pictures and images in Visual Basic .NET programming language.

2. How can I use VB.Net Picture Help in my project?

To use VB.Net Picture Help, you can either download and install the package into your Visual Studio environment, or simply include the necessary libraries and classes in your project. You can then reference these tools and resources in your code to manipulate and work with pictures.

3. What kind of picture operations can I perform with VB.Net Picture Help?

VB.Net Picture Help offers a wide range of operations for working with pictures, including resizing, cropping, rotating, converting file formats, applying filters and effects, and more. It also provides tools for working with metadata and EXIF information of images.

4. Is VB.Net Picture Help compatible with all versions of Visual Basic .NET?

Yes, VB.Net Picture Help is compatible with all versions of Visual Basic .NET, including the latest version. However, some features may only be available in certain versions or may require additional libraries to be installed.

5. Are there any resources available for learning VB.Net Picture Help?

Yes, there are many online tutorials, articles, and forums dedicated to helping developers learn and use VB.Net Picture Help. The official Microsoft documentation also offers in-depth explanations and examples of using the tools and resources provided in VB.Net Picture Help.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • General Discussion
Replies
2
Views
3K
  • Aerospace Engineering
Replies
2
Views
7K
  • Feedback and Announcements
Replies
0
Views
94K
Replies
11
Views
4K
  • Computing and Technology
Replies
6
Views
8K
Replies
266
Views
26K
Replies
1
Views
2K
  • General Discussion
Replies
1
Views
8K
Back
Top