Results 1 to 14 of 14

Thread: Can I draw to a PictBox outside of a paint event?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Can I draw to a PictBox outside of a paint event?

    Guys,

    I finally realized pros/cons of CreateGraphics Method vs. Paint event (with some help from members here) for a Picture box.

    I want program code that is doing calculations to to update several different PictureBoxes but it seems weird to have calculation code have to be inside a Paint event to paint the "data"...

    Structurally I am asking if I can have a program that calculates something and then updates multiple different Pictureboxes (mostly just drawing points or showing displaying data in a plot format)...

    Is there a method to call DrawRectangle() function from code running OUTSIDE of the PaintEvent and update a drawing in a specific PictureBox by passing the right parameters?

    Ideally I would have the main program running its calcs and when it gets a result to be displayed I can draw to pictBox a, b, c and so forth form the main program event (not from a Paint Event for each Picture Box).

    Right now the program runs the specific code within the Paint event itself...this becomes problematic when I do different calculations and want to print them to different PictureBoxes.
    Seems like I need to split up the calculations so that each calculation occurs in a different PictureBox Paint event...

    Appreciate any hints as to how to do this, assuming it is possible.
    Last edited by Swisskid; Oct 29th, 2024 at 06:06 PM. Reason: Clarity

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Can I draw to a PictBox outside of a paint event?

    To create persistent graphics outside of a paint event…

    Code:
    Dim img As Nee Bitmap(width, height)
    Dim gr As Graphics = Graphics.FromImage(img)
    
    gr.Draw… “etc
    
    PictureBox1.Image = img

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

    Re: Can I draw to a PictBox outside of a paint event?

    Quote Originally Posted by Swisskid View Post
    it seems weird to have calculation code have to be inside a Paint event to paint the "data"...
    So don't. Store the data in one or more fields and have the drawing code in the Paint event handler use that data. The code to calculate the data and set the fields can be elsewhere. That way, you don't have to do the same calculations over and over in order to do the same drawing. Any time you need to change the drawing, run the code to do the calculation and set the fields and then call Invalidate on the control to force a Paint event, which will erase the old drawing and redraw with the new data. Here's one I prepared earlier:

    https://www.vbforums.com/showthread....rawing-Program

    That stores the data for the lines to be drawn in a List at the class level. The Paint event handler simply draws whatever is in that List. If a new line needs to be added, the List is modified and a Paint event is triggered.
    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

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: Can I draw to a PictBox outside of a paint event?

    Thank you Paul - so drawing to a bitmap and then loading thru img. I had some trouble with that approach but will try again. Thank you!

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: Can I draw to a PictBox outside of a paint event?

    Thank you jmcilhinney...I think you and Paul may have commented on my questions regarding graphics previously. bmp handling is the last thing I need to master after PaintEvent and CreateGraphics & I think I will be able to do just about anything with the pictbox. Thanks for the help & the example!

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

    Re: Can I draw to a PictBox outside of a paint event?

    For the record, I would tend to use a Bitmap assigned to the Image property (or setting the ImageLocation for a file) if the image doesn't change at all or not very much. If the image changes relatively frequently especially to make modifications to an existing image, then I'd consider using the Paint event.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: Can I draw to a PictBox outside of a paint event?

    OK - great advice from you both - got it working in no time.

    My only remaining issue is that I would like to clear the screen prior to the next calculation results being displayed on the picture box. Right now every time I push the button new results are simply added to the existing results.

    Right now the code is event driven (button push "start calculation") and all the drawing code is where I want it and the image is persistent. My remaining issue is that I would like to clear the screen to the background color each time the button is pushed to make room for a second calculation => drawing result. Look back at past posts and tried "Pictbox.Image = nothing" and it did nothing (drawn image persists). I tried a combination of the previous with "Pictbox.image.dispose()" and this just removes the entire picture box - even tried drawing a black colored box over the entire previosly drawn image and this did not work either. Any suggestions?

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: Can I draw to a PictBox outside of a paint event?

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'gr.DrawRectangle(myPenCLS, 0, 0, 800, 800)<=== this is where I would like code that will clear teh screen but not get ride of the picture box
    'PictureBox1.Image = Nothing

    While LoopIndex < 100000
    radius = CryptoRadius() : psi = CryptoPsi() : psiradians = psi * Math.PI / 180
    rx = CInt(radius * Math.Cos(psiradians)) : ry = CInt(radius * Math.Sin(psiradians))
    gr.DrawRectangle(myPen2, Math.Round(400 + rx, 0), Math.Round(400 + ry, 0), 1, 1)
    LoopIndex = LoopIndex + 1
    End While

    LoopIndex = 0

    PictureBox1.Image = img
    PictureBox1.Refresh()

    End Sub

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Can I draw to a PictBox outside of a paint event?

    Code:
    gr.Clear(a color)

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Can I draw to a PictBox outside of a paint event?

    If you just want to clear a rectangular part of the image, you can use gr.FillRectangle( ' etc

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

    Re: Can I draw to a PictBox outside of a paint event?

    Quote Originally Posted by Swisskid View Post
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    'gr.DrawRectangle(myPenCLS, 0, 0, 800, 800)<=== this is where I would like code that will clear teh screen but not get ride of the picture box
    'PictureBox1.Image = Nothing

    While LoopIndex < 100000
    radius = CryptoRadius() : psi = CryptoPsi() : psiradians = psi * Math.PI / 180
    rx = CInt(radius * Math.Cos(psiradians)) : ry = CInt(radius * Math.Sin(psiradians))
    gr.DrawRectangle(myPen2, Math.Round(400 + rx, 0), Math.Round(400 + ry, 0), 1, 1)
    LoopIndex = LoopIndex + 1
    End While

    LoopIndex = 0

    PictureBox1.Image = img
    PictureBox1.Refresh()

    End Sub
    It's hard to say for sure but, based on that code, it looks like you may be creating one Graphics object and kjeeping it around. Don't do that. Create a Grpahics object when you need one and then destroy it when you're done with it. Create another one later if you need to draw again later. The same goes for a Pen or Brush object, if you're not using the standard, system-suplied ones.
    Code:
    Using g = Graphics.FromImage(myImage),
          p As New Pen(someColor)
        'Use g and p here.
    End Using
    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

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: Can I draw to a PictBox outside of a paint event?

    Hi Paul,

    Thank you! BTW I tried just filling with a color but stupidly used DrawRect vs. FillRect.
    Perfect.

    Thank you!

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jun 2024
    Posts
    18

    Re: Can I draw to a PictBox outside of a paint event?

    Thank you jmcilhinney. Yes I understand that - that was the next step ...

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Can I draw to a PictBox outside of a paint event?

    This will help you understand PictureBoxes...

    https://www.vbforums.com/showthread....86#post5655986

Tags for this Thread

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