Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

VB.net Picture Help

  1. Nov 2, 2009 #1

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    Hey guys,

    Working on a program.

    My program mostly takes place on a Picture Box where a user can click the picture in different areas and different images and sentences and words come up.

    Hence, I used the following often...

    g = PictureBox1.CreateGraphics()
    g.DrawString("Hello World", font, brush, 50, 50)

    But the images clear if the box is moved over anything. Hence a user who's playing and then decides to send a message to his friend will have his Picture cleared. How can I keep this from happening?
     
  2. jcsd
  3. Nov 2, 2009 #2

    DavidSnider

    User Avatar
    Gold Member

    Are you redrawing the box when it gets moved?
     
  4. Nov 2, 2009 #3

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    Oh no, the stuff is already plotted.

    THen you move it, and it gets erased and teh default picture is shown.
     
  5. Nov 2, 2009 #4

    DavidSnider

    User Avatar
    Gold Member

    Try drawing to an in-memory bitmap and then on the OnDraw event of the picturebox set the picturebox bitmap to the memory bitmap.
     
  6. Nov 2, 2009 #5
    You need to have a "paint event" for the picture box and draw the string there. That way any time the picture box redrawn your text will be redrawn also. Example:

    Code (Text):

    Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

            e.Graphics.DrawString("Hello world", Font, brush, 50, 50)

    End Sub
     
  7. Nov 2, 2009 #6

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    Well, then this draws it right away. Here is an idea of what I'm doing...

    So, the event only occurs if they click on it
     
  8. Nov 2, 2009 #7

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    I tried doing that before and my code always had errors.
     
  9. Nov 2, 2009 #8
    You can use logic in the paint event.

    Code (Text):
    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

            PaintMyString = True

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

            If PaintMyString = True Then
                e.Graphics.DrawString("Hello world", Font1, Brushes.Black, 20, 20)
            End If

    End Sub
     
  10. Nov 2, 2009 #9

    DavidSnider

    User Avatar
    Gold Member

    I don't think you want to redraw the memory bitmap every time Paint is called (unless your image is dynamic in some way). I'd draw it to memory once and then redraw the bitmap.

    DrawString takes a lot of cycles compared to a plain ol' blit.
     
  11. Nov 2, 2009 #10

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    I have no idea how to do even that.
     
  12. Nov 2, 2009 #11
    I think this method will accomplish what DavidSnider is talking about. Put the code in your mousedown event.

    Code (Text):
    g = Graphics.FromImage(PictureBox1.Image)
    g.DrawString("Hello World", font, brush, 50, 50)
    PictureBox1.Refresh()
     
  13. Nov 6, 2009 #12

    JasonRox

    User Avatar
    Homework Helper
    Gold Member

    I'm going to try that out. Thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: VB.net Picture Help
  1. Vb.net\ Gui (Replies: 5)

  2. VB.net VS visual C (Replies: 1)

Loading...