View Full Version : VB.net Picture Help
JasonRox
Nov2-09, 02:29 PM
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?
DavidSnider
Nov2-09, 02:32 PM
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?
JasonRox
Nov2-09, 02:38 PM
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.
DavidSnider
Nov2-09, 02:45 PM
Try drawing to an in-memory bitmap and then on the OnDraw event of the picturebox set the picturebox bitmap to the memory bitmap.
TurtleMeister
Nov2-09, 02:53 PM
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:
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
JasonRox
Nov2-09, 02:58 PM
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:
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
JasonRox
Nov2-09, 02:59 PM
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.
TurtleMeister
Nov2-09, 03:04 PM
You can use logic in the paint event.
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
DavidSnider
Nov2-09, 03:11 PM
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.
JasonRox
Nov2-09, 03:43 PM
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.
TurtleMeister
Nov2-09, 07:42 PM
I think this method will accomplish what DavidSnider is talking about. Put the code in your mousedown event.
g = Graphics.FromImage(PictureBox1.Image)
g.DrawString("Hello World", font, brush, 50, 50)
PictureBox1.Refresh()
JasonRox
Nov6-09, 11:20 AM
I think this method will accomplish what DavidSnider is talking about. Put the code in your mousedown event.
g = Graphics.FromImage(PictureBox1.Image)
g.DrawString("Hello World", font, brush, 50, 50)
PictureBox1.Refresh()
I'm going to try that out. Thanks!
vBulletin® v3.7.6, Copyright ©2000-2009, Jelsoft Enterprises Ltd.