Persist Picturebox Graphics

Lucky

New member
Joined
Oct 16, 2003
Messages
4
Location
Boulder,CO
I load a .jpg file into a picturebox then allow the user to draw graphics(line, rect, polygon, etc). I then want to save the image and persist the graphics. In VB6, setting AutoRedraw = true would do this. In VB dot net the AutoRedraw property does not exist. Documentation indicates that you must draw the graphics within the Paint event in order for them to persist. Ive tried this but it doesnt work. Also, calling Image.Save(path) saves the image as it was, but without the added graphics.

Does anyone know how to persist graphics within a picturebox?
 
The documentation was right. As long as you do all your drawing in the Paint event (and no-where else), it will persist the graphics.
 
For the second question, make sure you draw to the actual image contained in the PixrueBox and not the PictureBox itself.
 
Thanks for your responses. Im still not getting it so heres the sample code Im using. Picture is shown in the picturebox, the graphic gets drawn but the resulting saved image does not have the graphic on it.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PictureBox1.Image = Image.FromFile("c:\ChangeMe.jpg")
End Sub

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

Dim Pen As Pen = New Pen(Color.Red, 6)
e.Graphics.DrawLine(Pen, 0, 0, 200, 200)

End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.PictureBox1.Refresh()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.PictureBox1.Image.Save("C:\Changed.jpg")
End Sub
 
There are many threads about this same thing on this forum. You must create a new Bitmap and then get a graphics obejct from the Bitmap. Not the picturebox.
 
I got it. Thanks a million. I was drawing to the picturebox and not the image. What I was missing was how to get the graphics object from the image itself. Heres the code.

Dim Pen As Pen = New Pen(Color.Red, 6)
Dim Bitmap As New Bitmap(Me.PictureBox1.Image)
Dim g As Graphics = Graphics.FromImage(Bitmap)

g.DrawLine(Pen, 0, 0, 800, 800)
Me.PictureBox1.Image = Bitmap

I had tried dim g as new Graphics.?? but that doesnt bring up the .Graphics property. Since Im new to this Ill have to look up the difference.
 
Back
Top