Results 1 to 13 of 13

Thread: [RESOLVED] Visual Basic 2010 2D Graphics

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Resolved [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)

  2. #2
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Visual Basic 2010 2D Graphics

    Quote Originally Posted by DavesChillaxin View Post
    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?

  4. #4
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Re: Visual Basic 2010 2D Graphics

    Quote Originally Posted by CiscoSoft View Post
    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:
    1. Private Sub PictureBox1_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles PictureBox1.Click
    2.     Dim buf As New Bitmap(PictureBox1.DisplayRectangle.Width, PictureBox1.DisplayRectangle.Height) 'Buffer image.
    3.     PictureBox1.DrawToBitmap(buf, PictureBox1.DisplayRectangle)
    4.     Dim graphic As Graphics = Graphics.FromImage(buf)
    5.  
    6.     'Here is where you do your drawing with the new graphics.
    7.     graphic.DrawLine(Pens.Blue, New Point(0,PictureBox1.PointToClient(Cursor.Position).Y), New Point(PictureBox1.Width,PictureBox1.PointToClient(Cursor.Position).Y))
    8.  
    9.     PictureBox1.Image = buf 'Replace the original image with the buffer.
    10. End Sub
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Visual Basic 2010 2D Graphics

    Quote Originally Posted by DavesChillaxin View Post
    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:
    1. Private Sub PictureBox1_Click( ByVal sender As System.Object,  ByVal e As System.EventArgs) Handles PictureBox1.Click
    2.     Dim buf As New Bitmap(PictureBox1.DisplayRectangle.Width, PictureBox1.DisplayRectangle.Height) 'Buffer image.
    3.     PictureBox1.DrawToBitmap(buf, PictureBox1.DisplayRectangle)
    4.     Dim graphic As Graphics = Graphics.FromImage(buf)
    5.  
    6.     'Here is where you do your drawing with the new graphics.
    7.     graphic.DrawLine(Pens.Blue, New Point(0,PictureBox1.PointToClient(Cursor.Position).Y), New Point(PictureBox1.Width,PictureBox1.PointToClient(Cursor.Position).Y))
    8.  
    9.     PictureBox1.Image = buf 'Replace the original image with the buffer.
    10. 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
    Attached Images Attached Images  

  6. #6
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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.
    Last edited by DavesChillaxin; Nov 15th, 2011 at 05:45 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Visual Basic 2010 2D Graphics

    Quote Originally Posted by DavesChillaxin View Post
    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()

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Visual Basic 2010 2D Graphics

    Quote Originally Posted by CiscoSoft View Post
    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.

  9. #9
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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.
    Last edited by DavesChillaxin; Nov 15th, 2011 at 05:56 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2011
    Location
    London, UK
    Posts
    16

    Re: Visual Basic 2010 2D Graphics

    Quote Originally Posted by DavesChillaxin View Post
    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

  11. #11
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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.
    Attached Images Attached Images  
    Last edited by DavesChillaxin; Nov 15th, 2011 at 07:01 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    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.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width