Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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. I've tried this but it doesn't 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?

  • *Experts*
Posted
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.
Posted

Thanks for your responses. I'm still not getting it so here's the sample code I'm 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

Posted

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. Here's 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 doesn't bring up the .Graphics property. Since I'm new to this I'll have to look up the difference.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...