none
How to initialize the Drawing content in the PictureBox? RRS feed

  • Question

  • Hi,

    I'm moving from VB6 to VB .net (or better refers to Visual Basic 2017). I have a problem to find the right syntax to use. For example, in this simple example, right after I have click the Button1, I want to clear the drawing in the Picturebox1 before clicking the Button2.

    +++++++++++++++++++++++++++++++++++++++++++++++++++++++

    Imports System.Drawing.Drawing2D
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim MyGraphics As Graphics = PictureBox1.CreateGraphics
            Dim MyPen1 As New Pen(Color.Red, 1)

            MyGraphics.DrawEllipse(MyPen1, 10, 80, 500, 300)

        End Sub

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Dim MyGraphics As Graphics = PictureBox1.CreateGraphics
            Dim MyPen2 As New Pen(Color.Blue, 1)

            MyGraphics.DrawRectangle(MyPen2, 10, 70, 50, 80)

        End Sub
    End Class

    +++++++++++++++++++++++++++++++++++++++++++++++++++++

    As I remember, there's a cls in VB6 help to do so. Unfortunately, it does not apply to VB.net.

    Can anyone kindly help? Thanks.

    Jason

    Sunday, August 19, 2018 10:40 AM

Answers

  • You should draw in the paint event like I showed you.

    You are not persisting the image. If you cover the form or resize over the picture it will be erased.

    Anyhow, add this to the start of your drawing code clear the window to the form backcolor (or add a color you want).

    MyGraphics.Clear(PictureBox1.backcolor)

    • Marked as answer by jasonfan2000 Sunday, August 19, 2018 12:19 PM
    Sunday, August 19, 2018 11:38 AM

All replies

  • You should draw in the paint event like I showed you.

    You are not persisting the image. If you cover the form or resize over the picture it will be erased.

    Anyhow, add this to the start of your drawing code clear the window to the form backcolor (or add a color you want).

    MyGraphics.Clear(PictureBox1.backcolor)

    • Marked as answer by jasonfan2000 Sunday, August 19, 2018 12:19 PM
    Sunday, August 19, 2018 11:38 AM
  • You should draw in the paint event like I showed you.

    You are not persisting the image. If you cover the form or resize over the picture it will be erased.

    Anyhow, add this to the start of your drawing code clear the window to the form backcolor (or add a color you want).

    MyGraphics.Clear(PictureBox1.backcolor)

    Hi, Tommy

    Thank you very much again. I've got what I want now. Like I said, I don't want to complicate the case for asking question.

    I will try to draw in the way that you've shown later on. For the moment, the syntax that you've shown is very clear-->MyGraphics.Clear

    All the Best,

    Jason

    Sunday, August 19, 2018 12:19 PM