Whenever I draw a line in Visual Basic 2010 in a picturebox, the old drawing disappears. how can I keep all the previous drawings as well as the new one? I am using this code: e.Graphics.DrawLine(Pens.Red, ponit1, point2)
Simply you'll have to draw everything you need in a single shot. I solved this myself by have a collection of some sort that holds all the objects to draw and to be drawn. If you drawing a new line, add the new lines specifications to this collection instead of actually drawing it.. When the paint event goes into action(by itself or directly by calling invalidate/refresh) it loops through this collection of objects to be drawn, and draws them as such.
Another method I've considered but have yet to try is copying the image your drawing to, then draw onto that image. Once you're done drawing to the stored image, simply place it back - replacing the original image.
Please rate if my post was helpful!
Per favore e grazie!
Simply you'll have to draw everything you need in a single shot. I solved this myself by have a collection of some sort that holds all the objects to draw and to be drawn. If you drawing a new line, add the new lines specifications to this collection instead of actually drawing it.. When the paint event goes into action(by itself or directly by calling invalidate/refresh) it loops through this collection of objects to be drawn, and draws them as such.
Another method I've considered but have yet to try is copying the image your drawing to, then draw onto that image. Once you're done drawing to the stored image, simply place it back - replacing the original image.
Thanks for your reply, as I'm new, could u please tell me what sort of codes should I use in order to apply the second method?
'Here is where you do your drawing with the new graphics.
graphic.DrawLine(Pens.Blue, New Point(0,PictureBox1.PointToClient(Cursor.Position).Y), New Point(PictureBox1.Width,PictureBox1.PointToClient(Cursor.Position).Y))
PictureBox1.Image = buf 'Replace the original image with the buffer.
End Sub
Please rate if my post was helpful!
Per favore e grazie!
'Here is where you do your drawing with the new graphics.
graphic.DrawLine(Pens.Blue, New Point(0,PictureBox1.PointToClient(Cursor.Position).Y), New Point(PictureBox1.Width,PictureBox1.PointToClient(Cursor.Position).Y))
PictureBox1.Image = buf 'Replace the original image with the buffer.
End Sub
Thanks, I think with the given codes I can solve the disappearance problem. But there is another problem. the frame (picturebox1) goes into a loop and keeps copying its self forever and each time changes its location. attached is a screen shot of my result. actually it creates an animation where the frame goes toward south-east. I want it to be fixed.
Thanks in Advance
Yes. FYI, in another method with will be called before paint event I have picturebox1.Refresh()
I can give u my team viewer details. If got time plz help me. Its now 4 days I'm working on this issue as I'm new, this is making me nuts. I need to finish this before my Uni assignment deadline.
Take the code out of the paint event. Your producing a never ending loop by doing so. This would produce what you're witnessing. So rather having it in the paint event, use it on a click event or something of the sort. Run that code ONLY when you want to add another line and not during the picturebox's paint event. The code I provided actually takes place of the paint event for your picture box at least in this case for drawing the lines.
This is my guess.
Last edited by DavesChillaxin; Nov 15th, 2011 at 05:56 PM.
Please rate if my post was helpful!
Per favore e grazie!
Take the code out of the paint event. Your producing a never ending loop by doing so. This would produce what you're witnessing. So rather having it in the paint event, use it on a click event or something of the sort. Run that code ONLY when you want to add another line and not during the picturebox's paint event. The code I provided actually takes place of the paint event for your picture box at least in this case for drawing the lines.
This is my guess.
WoW, you are genius dude, job done but with one flaw. the picturebox still going south-east but this time not automatically, it goes one step each time I draw an new line. So there is no problem with 3 or 4 draws (as it is still in the frame) but lets say if I try to draw 15 lines (15 Clicks) the picturebox would be gone LoL
No problem, I'm always glad to help others. But still I'm a little baffled as to why it's doing this.. hmmm, is your buffer image the exact same size as your picture box?
Just checking but if you create a new project and paste my code in it along with a picture box, this anomaly your experiencing doesn't occur. It has to be something in your code. Can you post some of your code that you have to draw these lines?
My code produces the image attached, which you can see does not have the same effect your receiving.
Last edited by DavesChillaxin; Nov 15th, 2011 at 07:01 PM.
Please rate if my post was helpful!
Per favore e grazie!
You might like to follow the CodeBank link in signature and check out my Simple Drawing thread. It shows how to draw temporarily on a PictureBox and then permanently transfer that drawing to the Image in the PictureBox. Think of a photo frame with a photo in it. Drawing on the glass of the photo frame doesn't affect the photo. This is similar.
Could that solve his issue? I mean what I gave him used the same picturebox, but it would make much more sense to do it your way.
I'm still curious as to why he had this "screen shot of a program taking screen shot of itself" effect. As though it's coping itself inside of itself causing a never ending stack which appears shifting to the lower right hand corner.
Please rate if my post was helpful!
Per favore e grazie!