[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Clear drawn image from a picturebox

Posted on 2009-04-22
Medium Priority
1,452 Views
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
``````
0
Question by:si2030
• 2

LVL 48

Accepted Solution

Wayne Taylor (webtubbs) earned 2000 total points
ID: 24204052
Call the Refresh method of the PictureBox....

pctWaveDisplay.Refresh()

...which forces a redraw of the control.

Wayne
0

Author Closing Comment

ID: 31618497
Wayne... Thankyou :)
0

LVL 86

Expert Comment

ID: 24205463
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.
0

LVL 86

Expert Comment

ID: 24205540
Nevermind...I see you have another question open where ABEL has already made these suggestions for you!  =)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Integration Management Part 2
Loops Section Overview
###### Suggested Courses
Course of the Month20 days, 2 hours left to enroll