[RESOLVED] Visual Basic 2010 2D Graphics
How to keep drawing in Visual Basic 2010?
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)
Re: Visual Basic 2010 2D Graphics
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.
Re: Visual Basic 2010 2D Graphics
Quote:
Originally Posted by
DavesChillaxin
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?
Re: Visual Basic 2010 2D Graphics
Quote:
Originally Posted by
CiscoSoft
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?
I put this together quick, it works but simply it's the best I could do at work in a short period of time.
vbnet Code:
Private Sub PictureBox1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Dim buf As New Bitmap(PictureBox1.DisplayRectangle.Width, PictureBox1.DisplayRectangle.Height) 'Buffer image.
PictureBox1.DrawToBitmap(buf, PictureBox1.DisplayRectangle)
Dim graphic As Graphics = Graphics.FromImage(buf)
'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
1 Attachment(s)
Re: Visual Basic 2010 2D Graphics
Quote:
Originally Posted by
DavesChillaxin
I put this together quick, it works but simply it's the best I could do at work in a short period of time.
vbnet Code:
Private Sub PictureBox1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Dim buf As New Bitmap(PictureBox1.DisplayRectangle.Width, PictureBox1.DisplayRectangle.Height) 'Buffer image.
PictureBox1.DrawToBitmap(buf, PictureBox1.DisplayRectangle)
Dim graphic As Graphics = Graphics.FromImage(buf)
'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
Re: Visual Basic 2010 2D Graphics
are you putting the code I provided into the picturebox's paint event?
I might need to see some of the code that's producing this.
Re: Visual Basic 2010 2D Graphics
Quote:
Originally Posted by
DavesChillaxin
are you putting the code I provided into the picturebox's paint event?
Yes. FYI, in another method with will be called before paint event I have picturebox1.Refresh()
Re: Visual Basic 2010 2D Graphics
Quote:
Originally Posted by
CiscoSoft
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.
Re: Visual Basic 2010 2D Graphics
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. :p
Re: Visual Basic 2010 2D Graphics
Quote:
Originally Posted by
DavesChillaxin
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. :p
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
1 Attachment(s)
Re: Visual Basic 2010 2D Graphics
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.
Re: Visual Basic 2010 2D Graphics
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.
Re: Visual Basic 2010 2D Graphics
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.