Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Error of Randomly clearing PictureBox

CitizenOlekCitizenOlek Posts: 53Member
Hello All,

I have a program that continuously randomly draws on a PictureBox object. I am using - Application.DoEvents() - to Catch the EXIT button click to stop the program. This does work, but it has the side-effect of randomly clearing the PictureBox every once in a while. Does anybody know how to stop this Call from clearing the PictureBox?
«1

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    : Hello All,
    :
    : I have a program that continuously randomly draws on a PictureBox
    : object. I am using - Application.DoEvents() - to Catch the EXIT
    : button click to stop the program. This does work, but it has the
    : side-effect of randomly clearing the PictureBox every once in a
    : while. Does anybody know how to stop this Call from clearing the
    : PictureBox?
    :
    can i see the code?
  • CitizenOlekCitizenOlek Posts: 53Member
    I've changed my code!
    Is there a polite way of cancelling a Thread?
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    : I've changed my code!
    : Is there a polite way of cancelling a Thread?
    :
    Sounds like you got it workin! Props, just leave the thread go it will get pushed off the first page and become forgetten shortly enough
  • CitizenOlekCitizenOlek Posts: 53Member
    Rather than start a new thread I am posting my new code here. I've got the program running pretty well. I can Pause,Start Over and Exit without crashing, but one glitch persists. The PictureBox Randomly clears itself every so often. I cannot see why.

    [code]
    Public Class Form1
    Dim _exit As Boolean = False
    Dim Timer1 As New System.Timers.Timer

    Dim bitmap As Bitmap
    Dim gr As Graphics

    Dim redBrush As New SolidBrush(Color.Red)
    Dim p As Pen = New Pen(Drawing.Color.White, 1)
    Dim theColor As Color = Drawing.Color.White
    Dim angle, A, B, many, effect, ecount, muchbl, drwidth As Integer
    Dim Xmin, Xmax, Ymin, Ymax, UC, pStatus, whButton As Integer
    Dim Ux, Uy As Single
    Dim Ran As New Random()

    Dim UclA(11) As Color
    Dim clA() As Color = _
    {Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, Color.Aquamarine, _
    Color.Azure, Color.Beige, Color.Bisque, Color.Black, _
    Color.BlanchedAlmond, _
    Color.Blue, Color.BlueViolet, Color.Brown, Color.BurlyWood, _
    Color.CadetBlue, Color.Chartreuse, Color.Chocolate, Color.Coral, _
    Color.CornflowerBlue, Color.Cornsilk, Color.Crimson, Color.Cyan, _
    Color.DarkBlue, Color.DarkCyan, Color.DarkGoldenrod, Color.DarkGray, _
    Color.DarkGreen, Color.DarkKhaki, Color.DarkMagenta, Color.DarkOliveGreen, _
    Color.DarkOrange, Color.DarkOrchid, Color.DarkOrchid, Color.DarkRed, _
    Color.DarkSalmon, Color.DarkSeaGreen, Color.DarkSlateBlue, Color.DarkTurquoise, _
    Color.DarkViolet, Color.DeepPink, Color.DeepSkyBlue, Color.DimGray, _
    Color.DodgerBlue, Color.Firebrick, Color.FloralWhite, Color.ForestGreen, _
    Color.Fuchsia, Color.Gainsboro, Color.GhostWhite, Color.Gold, _
    Color.Goldenrod, Color.Gray, Color.Green, Color.GreenYellow, _
    Color.Honeydew, Color.HotPink, Color.IndianRed, Color.Ivory, _
    Color.Khaki, Color.Lavender, Color.LavenderBlush, Color.LawnGreen, _
    Color.LemonChiffon, Color.LightBlue, Color.LightCoral, Color.LightCyan, _
    Color.LightGoldenrodYellow, Color.LightGray, Color.LightGreen, Color.LightPink, _
    Color.LightSalmon, Color.LightSeaGreen, Color.LightSkyBlue, Color.LightSkyBlue, _
    Color.LightSlateGray, Color.LightSteelBlue, Color.LightYellow, Color.Lime, _
    Color.LimeGreen, Color.Linen, Color.Magenta, Color.Maroon, _
    Color.MediumAquamarine, Color.MediumBlue, Color.MediumOrchid, Color.MediumPurple, _
    Color.MediumSeaGreen, Color.MediumSlateBlue, Color.MediumSpringGreen, Color.MediumTurquoise, _
    Color.MediumVioletRed, Color.MidnightBlue, Color.MintCream, Color.MistyRose, _
    Color.Moccasin, Color.NavajoWhite, Color.Navy, Color.OldLace, _
    Color.Olive, Color.OliveDrab, Color.Orange, Color.OrangeRed, _
    Color.Orchid, Color.PaleGoldenrod, Color.PaleGreen, Color.PaleTurquoise, _
    Color.PaleVioletRed, Color.PapayaWhip, Color.PeachPuff, Color.Peru, _
    Color.Pink, Color.Plum, Color.PowderBlue, Color.Purple, _
    Color.Red, Color.RosyBrown, Color.RoyalBlue, Color.SaddleBrown, _
    Color.Salmon, Color.SandyBrown, Color.SeaGreen, Color.SeaShell, _
    Color.Sienna, Color.Silver, Color.SkyBlue, Color.SlateBlue, _
    Color.SlateGray, Color.Snow, Color.SpringGreen, Color.SteelBlue, _
    Color.Tan, Color.Teal, Color.Thistle, Color.Tomato, _
    Color.Turquoise, Color.Violet, Color.Wheat, Color.White, _
    Color.WhiteSmoke, Color.Yellow, Color.YellowGreen}

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler Timer1.Elapsed, AddressOf TimerFired
    Randomize()
    bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
    gr = Graphics.FromImage(bitmap)
    PictureBox1.Image = bitmap
    Timer1.Interval = 50 ' 1000 = 1 second
    Xmin = 20
    Xmax = PictureBox1.Width - 20
    Ymin = 20
    Ymax = PictureBox1.Height - 20
    Ux = Ran.Next(Xmax - Xmin - 40) + Xmin + 20
    Uy = Ran.Next(Ymax - Ymin - 40) + Ymin + 20
    many = Ran.Next(4991) + 10
    theColor = clA(Ran.Next(140))
    p.Color = theColor
    UC = 0
    effect = 1
    drwidth = 1
    muchbl = 5
    pStatus = 0
    whButton = 0
    ecount = Ran.Next(20) + 1
    B = 1
    End Sub

    Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
    For A = 1 To many
    DoMain()
    Next
    If whButton = 10 Then ' Exit Program
    End
    End If
    If whButton = 2 Then ' Pause
    Timer1.Enabled = False
    whButton = 0
    End If
    If whButton = 3 Then ' Start Over
    PictureBox1.CreateGraphics().Clear(Color.Black)
    whButton = 1
    End If
    If B > ecount Then
    ecount = Ran.Next(20) + 1
    B = 0
    End If
    B += 1
    If UC > 0 Then
    theColor = UclA(Ran.Next(UC) + 1)
    Else : theColor = clA(Ran.Next(140))
    End If

    drwidth = 1
    'If ((Int((muchbl - 1 + 1) * Rnd() + 1)) - 1) = 3 Then
    ' ' A 1 in muchbl chance of using Black
    ' theColor = Color.Black
    ' drwidth = 8
    'End If
    p.Color = theColor
    many = Ran.Next(4991) + 10
    effect = Ran.Next(2) + 1
    ecount = Ran.Next(20) + 1
    End Sub

    Private Sub DoMain()
    Timer1.Enabled = False
    angle = Ran.Next(8) + 1
    Select Case angle
    Case 1
    Uy -= 1
    Case 2
    Ux += 1
    Uy -= 1
    Case 3
    Ux += 1
    Case 4
    Ux += 1
    Uy += 1
    Case 5
    Uy += 1
    Case 6
    Ux -= 1
    Uy += 1
    Case 7
    Ux -= 1
    Case 8
    Ux -= 1
    Uy -= 1
    End Select
    If Ux < Xmin Or Ux > Xmax Or Uy < Ymin Or Uy > Ymax Then
    Ux = Ran.Next(Xmax - Xmin - 40) + Xmin + 20
    Uy = Ran.Next(Ymax - Ymin - 40) + Ymin + 20
    End If
    PictureBox1.CreateGraphics().DrawEllipse(p, Ux, Uy, 1, drwidth)
    Select Case effect
    Case 1

    Case 2
    If bitmap.GetPixel(Ux, Uy - 1) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux, Uy - 1, 1, drwidth)
    ElseIf bitmap.GetPixel(Ux + 1, Uy - 1) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux + 1, Uy - 1, 1, drwidth)
    ElseIf bitmap.GetPixel(Ux + 1, Uy) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux + 1, Uy, 1, drwidth)
    ElseIf bitmap.GetPixel(Ux + 1, Uy + 1) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux + 1, Uy + 1, 1, drwidth)
    ElseIf bitmap.GetPixel(Ux, Uy + 1) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux, Uy + 1, 1, drwidth)
    ElseIf bitmap.GetPixel(Ux - 1, Uy + 1) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux - 1, Uy + 1, 1, drwidth)
    ElseIf bitmap.GetPixel(Ux - 1, Uy) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux - 1, Uy, 1, drwidth)
    ElseIf bitmap.GetPixel(Ux - 1, Uy - 1) <> theColor Then
    PictureBox1.CreateGraphics().DrawEllipse(Pens.Black, Ux - 1, Uy - 1, 1, drwidth)
    End If
    Case 3
    End Select
    Timer1.Enabled = True
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    pStatus = 1
    whButton = 1
    Timer1.Enabled = True
    End Sub
    Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click
    If whButton = 0 Then
    Timer1.Enabled = True
    whButton = 1
    Else : whButton = 2
    End If
    End Sub
    Private Sub btnStartOver_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartOver.Click
    whButton = 3
    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    If Timer1.Enabled Then
    whButton = 10
    Else : End
    End If
    End Sub

    End Class
    [/code]
  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : Rather than start a new thread I am posting my new code here. I've
    : got the program running pretty well. I can Pause,Start Over and Exit
    : without crashing, but one glitch persists. The PictureBox Randomly
    : clears itself every so often. I cannot see why.
    :

    Crazy thought:
    Probably everytime you call CreateGraphics() it repaints the changes made to it, and then returns a newly updates Graphics object. Meaning, you are forcing a repaint every time you call CreateGraphics. The solution is to use one main Graphics object or something, that is created from the PictureBox.

    Well it's a theory - up to you to find out if it's correct ;)

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • CitizenOlekCitizenOlek Posts: 53Member
    : Crazy thought:
    : Probably everytime you call CreateGraphics() it repaints the changes
    : made to it, and then returns a newly updates Graphics object.
    : Meaning, you are forcing a repaint every time you call
    : CreateGraphics. The solution is to use one main Graphics object or
    : something, that is created from the PictureBox.
    :

    At its core this program is simply drawing a point on the PictureBox and letting you watch as it does so.

    PictureBox1.CreateGraphics().DrawEllipse(p, Ux, Uy, 1, drwidth)

    Then it draws another point, etc.

    Using - gr.DrawEllipse(p, Ux, Uy, 1, drwidth) - draws to the graphics object (gr), but then you need to use - PictureBox1.Refresh() - or PictureBox1. Invalidate() - to see what you've drawn. I've discovered through implementation that rapidly drawing this way doesn't work. I don't know why but the PictureBox will remain blank until you stop drawing at which point all the drawing will appear.

    Is the use of the line:

    PictureBox1.CreateGraphics().DrawEllipse(p, Ux, Uy, 1, drwidth)

    wrong? Is it using up memory every time it's called?

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member

    : At its core this program is simply drawing a point on the PictureBox
    : and letting you watch as it does so.
    :
    : PictureBox1.CreateGraphics().DrawEllipse(p, Ux, Uy, 1, drwidth)
    :
    : Then it draws another point, etc.
    :
    : Using - gr.DrawEllipse(p, Ux, Uy, 1, drwidth) - draws to the
    : graphics object (gr), but then you need to use -
    : PictureBox1.Refresh() - or PictureBox1. Invalidate() - to see what
    : you've drawn. I've discovered through implementation that rapidly
    : drawing this way doesn't work. [italic]I don't know why but the PictureBox
    : will remain blank until you stop drawing at which point all the
    : drawing will appear.[/italic]

    Be very glad it does: it's called Optimization and speeds up your code considerably!

    :
    : Is the use of the line:
    :
    : PictureBox1.CreateGraphics().DrawEllipse(p, Ux, Uy, 1, drwidth)
    :
    : wrong? Is it using up memory every time it's called?
    :

    Well you already have the gr object right? So why not use that?

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • CitizenOlekCitizenOlek Posts: 53Member
    : Well you already have the gr object right? So why not use that?

    That's good advice, I should use the gr object, but is there a way I can use the gr object while retaining the ability to watch the drawing process?

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    : That's good advice, I should use the gr object, but is there a way I
    : can use the gr object while retaining the ability to watch the
    : drawing process?
    :
    :

    Perhaps timed drawing?
    If watching the draw process is an integral part of the application, you should use some sort of timer to draw successive parts of the picture.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • CitizenOlekCitizenOlek Posts: 53Member

    : Perhaps timed drawing?
    : If watching the draw process is an integral part of the application,
    : you should use some sort of timer to draw successive parts of the
    : picture.
    :

    I am using a Timer Event to do the drawing! I was just wondering if anyone could figure out why the program was randomly clearing the PictureBox. Sometimes after only a second or two and sometimes after a minute.
«1
Sign In or Register to comment.