Link to home
Start Free TrialLog in
Avatar of si2030
si2030

asked on

Clear drawn image from a picturebox

Hi Experts

I have a picturebox in which I draw a sinewave or a squarewave depending on which radiobutton is clicked on.

I have included the two subroutines below.

My problem is that one draws over the other if I click on first one radiobutton and then the other. How do I clear the first drawing ready to draw the next??

The routines idealy should have something like:

pctWaveDisplay.clear

but there is nothing I can find.

Kind Regards

Simon
Private Sub drawSineWave()
 
 
        'I just draw a Sin graphics between 0-2p for example 
 
        Dim x0 As Single = 80.0F
 
        Dim y0 As Single = 50.0F
 
 
        'Assume the graphics width is 200pixels 
 
        'so there're 200 points 
 
        Dim points As PointF() = New PointF(199) {}
 
        For j As Integer = 0 To 199
 
 
            points(j) = New PointF()
 
            points(j).X = x0 + j
 
 
            points(j).Y = y0 - CSng((Math.Sin((2 * Math.PI * j) / 200) * (200 / (2 * Math.PI))))
 
        Next
 
        Using p As New Pen(Color.Blue)
 
            p.EndCap = LineCap.ArrowAnchor
 
            'Draw X-coordinate 
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0, x0 + 220, y0)
 
            'Draw(Y - coordinate)
 
            p.StartCap = LineCap.ArrowAnchor
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0 + 47, x0, y0 - 47)
 
            p.DashStyle = DashStyle.Dash
 
            p.Color = Color.Red
 
            p.StartCap = LineCap.NoAnchor
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0 - 32, x0 + 50, y0 - 32)
 
 
        End Using
 
        pctWaveDisplay.CreateGraphics.DrawString("0", SystemFonts.DefaultFont, Brushes.Blue, x0, y0)
 
        pctWaveDisplay.CreateGraphics.DrawString("p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 95, y0)
 
        pctWaveDisplay.CreateGraphics.DrawString("2p", SystemFonts.DefaultFont, Brushes.Blue, x0 + 200, y0)
 
        pctWaveDisplay.CreateGraphics.DrawString("VOLTAGE", SystemFonts.DefaultFont, Brushes.Blue, x0 - 60, y0 - 43)
 
        pctWaveDisplay.CreateGraphics.SmoothingMode = SmoothingMode.AntiAlias
 
        pctWaveDisplay.CreateGraphics.DrawLines(Pens.Red, points)
 
    End Sub
 
    Private Sub drawSquareWave()
 
        'I just draw a Sin graphics between 0-2p for example 
 
        Dim x0 As Single = 80.0F
 
        Dim y0 As Single = 50.0F
 
 
        'Assume the graphics width is 200pixels 
 
        'so there're 200 points 
 
 
        Using p As New Pen(Color.Blue)
 
            p.EndCap = LineCap.ArrowAnchor
 
            'Draw X-coordinate 
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0, x0 + 220, y0)
 
            'Draw(Y - coordinate)
 
            p.StartCap = LineCap.ArrowAnchor
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0 + 47, x0, y0 - 47)
 
            'Draw the square wave.
 
            p.StartCap = LineCap.NoAnchor
            p.EndCap = LineCap.NoAnchor
 
            p.Color = Color.Red
 
            p.DashStyle = DashStyle.Solid
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0 - 10, y0, x0, y0)
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0, x0, y0 - 32)
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0, y0 - 32, x0 + 100, y0 - 32)
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0 + 100, y0 - 32, x0 + 100, y0)
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0 + 100, y0, x0 + 200, y0)
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0 + 200, y0, x0 + 200, y0 - 32)
 
            p.EndCap = LineCap.ArrowAnchor
 
            pctWaveDisplay.CreateGraphics.DrawLine(p, x0 + 200, y0 - 32, x0 + 210, y0 - 32)
 
        End Using
 
        pctWaveDisplay.CreateGraphics.DrawString("0", SystemFonts.DefaultFont, Brushes.Red, x0, y0)
 
        pctWaveDisplay.CreateGraphics.DrawString("p", SystemFonts.DefaultFont, Brushes.Red, x0 + 95, y0)
 
        pctWaveDisplay.CreateGraphics.DrawString("2p", SystemFonts.DefaultFont, Brushes.Red, x0 + 200, y0)
 
        pctWaveDisplay.CreateGraphics.DrawString("VOLTAGE", SystemFonts.DefaultFont, Brushes.Red, x0 - 60, y0 - 43)
 
        pctWaveDisplay.CreateGraphics.SmoothingMode = SmoothingMode.AntiAlias
 
 
 
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of si2030
si2030

ASKER

Wayne... Thankyou :)
You should be MANUALLY disposing of the graphics returned by your calls to CreateGraphics().  Additionally, you should use only ONE for the duration of your drawing:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.creategraphics(VS.71).aspx

    "The returned Graphics object must be disposed through a call to its Dispose method when it is no longer needed."

So something like:

    Dim G As Graphics = pctWaveDisplay.CreateGraphics()

    ' do ALL of your drawing with "G" in here
    G.DrawLine(...)
    G.DrawString(...)
    ' etc...

    G.Dispose()

If you want the graphics to be PERSISTENT and not get erased when the window is minimized or another window obscures yours then use the graphics supplied for you in the Paint() event of the PictureBox in the "e" parameter.
Nevermind...I see you have another question open where ABEL has already made these suggestions for you!  =)