Lucky Posted October 18, 2003 Posted October 18, 2003 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? Quote
*Experts* Volte Posted October 18, 2003 *Experts* Posted October 18, 2003 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. Quote
*Experts* mutant Posted October 18, 2003 *Experts* Posted October 18, 2003 For the second question, make sure you draw to the actual image contained in the PixrueBox and not the PictureBox itself. Quote
Lucky Posted October 19, 2003 Author Posted October 19, 2003 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 Quote
aewarnick Posted October 19, 2003 Posted October 19, 2003 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. Quote C#
Lucky Posted October 19, 2003 Author Posted October 19, 2003 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.