VB.NET

Moderators: seancampbell
Number of threads: 4022
Number of posts: 10035

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Error of Randomly clearing PictureBox Posted by CitizenOlek on 21 Jun 2007 at 9:51 PM
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?
Report
Re: Error of Randomly clearing PictureBox Posted by seancampbell on 21 Jun 2007 at 11:02 PM
: 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?
Report
Re: Error of Randomly clearing PictureBox Posted by CitizenOlek on 22 Jun 2007 at 2:02 PM
I've changed my code!
Is there a polite way of cancelling a Thread?
Report
Re: Error of Randomly clearing PictureBox Posted by seancampbell on 22 Jun 2007 at 2:52 PM
: 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
Report
Re: Error of Randomly clearing PictureBox Posted by CitizenOlek on 23 Jun 2007 at 3:37 AM
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.

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

Report
Re: Error of Randomly clearing PictureBox Posted by BitByBit_Thor on 23 Jun 2007 at 4:41 AM
: 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
Report
Re: Error of Randomly clearing PictureBox Posted by CitizenOlek on 23 Jun 2007 at 11:01 AM
: 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?

Report
Re: Error of Randomly clearing PictureBox Posted by BitByBit_Thor on 24 Jun 2007 at 4:24 AM

: 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.


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
Report
Re: Error of Randomly clearing PictureBox Posted by CitizenOlek on 24 Jun 2007 at 7:54 AM
: 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?

Report
Re: Error of Randomly clearing PictureBox Posted by BitByBit_Thor on 24 Jun 2007 at 8:22 AM
: 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
Report
Re: Error of Randomly clearing PictureBox Posted by CitizenOlek on 24 Jun 2007 at 7:50 PM

: 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.
Report
Re: Error of Randomly clearing PictureBox Posted by seancampbell on 25 Jun 2007 at 5:26 AM
I played with the code this morning, it appears that anytime another window is places above this one, it clears the graphics that are drawn on it. I speculate that occasionally windows refreshes and soes some sort of clearscreen that causes that form to lose its picture (I can cause it by giving the program focus, and then removing focus by clicking a program on my second monitor).
Report
Re: Error of Randomly clearing PictureBox Posted by seancampbell on 25 Jun 2007 at 6:15 AM
Ok. It was driving me nuts that I didn't know what was going on, so I went and figured out what was going on...

now this program may not be the best programming practices, but it works... I am not used to doing any type of animation, but it appeared like you were trying to do just that so i left your program 'animating'.

Basically, whenever you do PictureBox.createGraphics() you are drawing directly to the control (not inside the Picturebox1.BackgroundImage or .Image). Whenever the form invalidates it repaints and the graphics you drew on it are cleared off (because they are only temporary). Now if you were animating, you would constantly repainting and it would be fine, but since we are just drawing these colors on the form I decided to animate with an object set to Picturebox1.createGraphics() and do the exact same thing onto the Gr object so I could store the graphics we drew into a Bitmap in memory. Then every time the timer is called it resets picturebox1.backgroundimage to that bitmap object of what we have drawn so far so it won't lost it on repaint...

I changed the code ever so slightly, but it should work for you (I moved the Timer1.Enabled = False / True to outside of the DoMain function to increase performance). I also changed the Color <> Color if statements to .Equals because I have Visual Studio 2003 and the <> operator was not working for me.

Enjoy:
Public Class Form1
     Inherits System.Windows.Forms.Form


    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)
        GR.Clear(PictureBox1.BackColor)
        PictureBox1.BackgroundImage = 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)
        Timer1.Enabled = False

        For A = 1 To many
            DoMain()
        Next

        Timer1.Enabled = True

        PictureBox1.BackgroundImage = bitmap

        Select Case whButton
            Case 10 'Exit
                End
            Case 2 'Pause
                Timer1.Enabled = False
                whButton = 0
            Case 3 'Start Over
                PictureBox1.CreateGraphics().Clear(Color.Black)
                GR.Clear(Color.Black)
                whButton = 1
        End Select

        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()
        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

        Dim TEMPG As Graphics
        TEMPG = PictureBox1.CreateGraphics()
        TEMPG.DrawEllipse(p, Ux, Uy, 1, drwidth)
        GR.DrawEllipse(p, Ux, Uy, 1, drwidth)

        Select Case effect
            Case 1

            Case 2
                If Not bitmap.GetPixel(Ux, Uy - 1).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux, Uy - 1, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux, Uy - 1, 1, drwidth)
                ElseIf Not bitmap.GetPixel(Ux + 1, Uy - 1).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux + 1, Uy - 1, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux + 1, Uy - 1, 1, drwidth)
                ElseIf Not bitmap.GetPixel(Ux + 1, Uy).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux + 1, Uy, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux + 1, Uy, 1, drwidth)
                ElseIf Not bitmap.GetPixel(Ux + 1, Uy + 1).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux + 1, Uy + 1, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux + 1, Uy + 1, 1, drwidth)
                ElseIf Not bitmap.GetPixel(Ux, Uy + 1).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux, Uy + 1, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux, Uy + 1, 1, drwidth)
                ElseIf Not bitmap.GetPixel(Ux - 1, Uy + 1).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux - 1, Uy + 1, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux - 1, Uy + 1, 1, drwidth)
                ElseIf Not bitmap.GetPixel(Ux - 1, Uy).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux - 1, Uy, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux - 1, Uy, 1, drwidth)
                ElseIf Not bitmap.GetPixel(Ux - 1, Uy - 1).Equals(theColor) Then
                    TEMPG.DrawEllipse(Pens.Black, Ux - 1, Uy - 1, 1, drwidth)
                    GR.DrawEllipse(Pens.Black, Ux - 1, Uy - 1, 1, drwidth)
                End If
            Case 3
        End Select
        'PictureBox1.Image = bitmap
    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


Report
Re: Error of Randomly clearing PictureBox Posted by BitByBit_Thor on 25 Jun 2007 at 7:21 AM
: Ok. It was driving me nuts that I didn't know what was going on, so
: I went and figured out what was going on...
:

Do VB.NET controls still have the AutoRedraw property? Set it to true and problem is solved.
Best Regards,
Richard

The way I see it... Well, it's all pretty blurry
Report
Re: Error of Randomly clearing PictureBox Posted by seancampbell on 25 Jun 2007 at 7:45 AM
: : Ok. It was driving me nuts that I didn't know what was going on, so
: : I went and figured out what was going on...
: :
:
: Do VB.NET controls still have the AutoRedraw property? Set it to
: true and problem is solved.
: Best Regards,
: Richard
:
: The way I see it... Well, it's all pretty blurry

Well, I think that it only draws the data in the Image and Background image parts of the object. So the data we are drawing on top of the picturebox is just in memory and is not 'remembered' when the picture box must repaint. The code i posted addressed it by storing the changes to picturebox1 in a bitmap image.

As far as I can tell the controls do not have an AutoRedraw property...

-Sean c
Report
Re: Error of Randomly clearing PictureBox Posted by CitizenOlek on 25 Jun 2007 at 7:56 AM
: Ok. It was driving me nuts that I didn't know what was going on, so
: I went and figured out what was going on...
:

I just punched in your code and it works great!
Thank you for your trouble. I will study the changes you made. Several programs I have planned will require similar treatment.

Happy coding all.
Report
Re: Error of Randomly clearing PictureBox Posted by seancampbell on 25 Jun 2007 at 8:10 AM
: : Ok. It was driving me nuts that I didn't know what was going on, so
: : I went and figured out what was going on...
: :
:
: I just punched in your code and it works great!
: Thank you for your trouble. I will study the changes you
: made. Several programs I have planned will require similar treatment.
:
: Happy coding all.
:

If you have any questions about it, let me know. Pretty much it is exactly the same as yours, I just changed a couple if statements to use different Operators (If not GetPixel.Equals(Color) instead of <> operator) and made the doMain subroutine not enable and disable the timer rapidly (that could cause some funny problems, and possibly slow down the program).

The biggest thing is the Bitmap image that I am editting and setting Picturebox1.image equal to...

Glad we could help!!!
-Sean C.
Report
Re: Error of Randomly clearing PictureBox Posted by CitizenOlek on 25 Jun 2007 at 7:18 PM
Here's a question!
This code puts a Bitmap Image into a PictureBox
PictureBox1.BackgroundImage = bitmap

Is there a similar,quick, simple way of copying an Image from a PictureBox into a Bitmap?
Report
Re: Error of Randomly clearing PictureBox Posted by seancampbell on 25 Jun 2007 at 8:10 PM
: Here's a question!
: This code puts a Bitmap Image into a PictureBox
:
: PictureBox1.BackgroundImage = bitmap
:
: Is there a similar,quick, simple way of copying an Image from a
: PictureBox into a Bitmap?
:

Dim BMP as Bitmap = PictureBox1.Image

That should work, but when you do a PictureBox.CreateGraphics() it doesn't draw onto the image, it draws onto the object itself, that was why it wasn't redrawing when the form invalidated.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.