9 Replies - 417 Views - Last Post: 25 January 2013 - 08:01 AM Rate Topic: -----

#1 xilith117  Icon User is online

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 13-August 11

Brush Trouble

Posted 22 January 2013 - 07:34 PM

What I want my code to do is to randomly scribble in a picturebox, then whenever the brush goes over an area that has already been passed over it paints a darker shade over that spot. But right now I can't get the brush to scribble even one color. Can someone help me make this brush work, then make it paint darker over areas already painted? Thanks.

How I imagine the result to look:
Posted Image


My code so-far:
 Private Sub MapDraw()

        Dim x As Integer
        Dim y As Integer
        Dim xmax As Integer
        Dim clr As Integer
        Dim j As Integer



        bm = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using g As Graphics = Graphics.FromImage(bm)
            g.Clear(PictureBox1.BackColor)
        End Using
        ' clear residual image
        xmax = bm.Width - 1

        PictureBox1.Image = bm

        ' MsgBox(PictureBox1.BackColor.A & " " & PictureBox1.BackColor.R & " " & PictureBox1.BackColor.G & " " & PictureBox1.BackColor.B)/>/>
        brush1 = New SolidBrush(Color.Black)

        Do Until j = 1000
            j = j + 1
            ProgressBar1.Value = j
            Randomize()
            ' Generate random value between 1 and 6.
            Dim rand As Integer = CInt(Int((4 * Rnd()) + 1))
            If rand = 1 Then
                y = y + 1

            ElseIf rand = 2 Then
                y = y - 1

            ElseIf rand = 3 Then   'depending on random number,
                x = x + 1
                'add or subtract for x or y value 
            ElseIf rand = 4 Then
                x = x - 1
            End If

            If x > PictureBox1.Width Then
                x = PictureBox1.Width
            End If
            If y > PictureBox1.Height Then
                y = PictureBox1.Height
            End If

            PlotPoint(x, y, brush1)

        Loop


    End Sub
    Private Sub PlotPoint(ByRef x As Integer, ByRef y As Integer, ByRef hue As Brush)
        Dim gr As Graphics = Graphics.FromImage(PictureBox1.Image)
        Dim Dot As Rectangle
        Dim dotsize As Integer = 4

        Dot.X = x
        Dot.Y = y

       
        gr.FillEllipse(hue, Dot)


        PictureBox1.Refresh()
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Brush Trouble

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: Brush Trouble

Posted 22 January 2013 - 10:48 PM

Using the alpha channel of the aRGB in the Color structure you make semi-transparent colors and when they draw over a previous area it will give that density appearance. Using DrawLine or DrawCurve from the Graphics object from the points you make would work well. The Random class is more .Net than Rnd() and Randomize() methods.
Was This Post Helpful? 1
  • +
  • -

#3 xilith117  Icon User is online

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 13-August 11

Re: Brush Trouble

Posted 23 January 2013 - 09:31 PM

i'm sorry, can you show me how to use drawline?
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: Brush Trouble

Posted 23 January 2013 - 09:46 PM

Drawline method. Come on some simple googling on your part would have found this.

Check out the different overloads and pick the one that works for you. I paint in the paint event and use the e.Graphics object for drawing. If you need to draw outside of this event know that is does not persist with a refresh of the window or control - where the paint event paints what ever is in the paint event. It is a good practice to use objects for painting for persistence and use in other methods when needed. Also when you create a Graphics object dispose it or use the Using/End Using blocks.
Was This Post Helpful? 1
  • +
  • -

#5 xilith117  Icon User is online

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 13-August 11

Re: Brush Trouble

Posted 24 January 2013 - 08:30 AM

I got it to work but my brush is a plus sign instead of a circle...?
Was This Post Helpful? 0
  • +
  • -

#6 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: Brush Trouble

Posted 24 January 2013 - 08:55 AM

If you need help with the brush show some code.
Was This Post Helpful? 0
  • +
  • -

#7 xilith117  Icon User is online

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 13-August 11

Re: Brush Trouble

Posted 24 January 2013 - 02:28 PM

Here you go. ://> Thanks.
 Dim x As Integer
        Dim y As Integer
        Randomize()
        ' Generate random value between 1 and 100 to stat drawing from.
        Dim rand1 As Integer = CInt(Int((100 * Rnd()) + 1))
        Randomize()
        Dim rand2 As Integer = CInt(Int((100 * Rnd()) + 1))
        x = rand1
        y = rand2

      




      

        '  bm = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Using g As Graphics = Graphics.FromImage(bm)
            g.Clear(PictureBox1.BackColor)
        End Using
        ' clear residual image


        PictureBox1.Image = bm

        ' MsgBox(PictureBox1.BackColor.A & " " & PictureBox1.BackColor.R & " " & PictureBox1.BackColor.G & " " & PictureBox1.BackColor.B)/>/>
        brush1 = New SolidBrush(Color.Black)

        Do Until j = 100000

            Me.Text = (x & " " & y)

            j = j + 1
            ProgressBar1.Value = j
            Randomize()
            ' Generate random value between 1 and 4.
            Dim rand As Integer = CInt(Int((4 * Rnd()) + 1))
            If rand = 1 Then
                y = y + 1

            ElseIf rand = 2 Then
                y = y - 1

            ElseIf rand = 3 Then   'depending on random number,
                x = x + 1
                'add or subtract for x or y value 
            ElseIf rand = 4 Then
                x = x - 1
            End If

            If x > PictureBox1.Width Then
                x = x - 1
            End If
            If y > PictureBox1.Height Then
                y = y - 1
            End If
            If x < 0 Then
                x = 0
            End If
            If y < 0 Then
                y = 0
            End If

            PlotPoint(x, y, brush1)

        Loop


    End Sub

    Private Sub PlotPoint(ByRef x As Integer, ByRef y As Integer, ByRef hue As Brush)
        Dim myPen As New Pen(brush1, 20)
        Dim gr As Graphics = Graphics.FromImage(PictureBox1.Image)
        brush1 = New SolidBrush(Color.FromArgb(5, 50, 0, 0))
        Dot.X = x
        Dot.Y = y
        If firstplot Then
            Dot.X = x - dotsize
            Dot.Y = y - dotsize
            gr.FillEllipse(hue, Dot)
            PictureBox1.Refresh()
            firstplot = False
        Else
            Dim oldx As Integer = lastdot.X
            Dim oldy As Integer = lastdot.Y
            gr.DrawLine(myPen, oldx, oldy, x, y)
            PictureBox1.Refresh()
            lastdot.X = lastdot.X - dotsize / 2
            lastdot.Y = lastdot.Y - dotsize / 2
            gr.FillEllipse(brush1, lastdot)
            PictureBox1.Refresh()
            Dot.X = x - dotsize / 2
            Dot.Y = y - dotsize / 2
            gr.FillEllipse(hue, Dot)
            PictureBox1.Refresh()
        End If
        Dot.X = x
        Dot.Y = y
        lastdot = Dot
        lastbrush = hue
        PictureBox1.Image = bm
    End Sub

This post has been edited by xilith117: 24 January 2013 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: Brush Trouble

Posted 24 January 2013 - 05:12 PM

Never call Refresh in a sub where your painting - poor performance. Your not disposing your graphics objects = memory leaks. Since you are making an image with each new line you can get away with just having a sub routine handle making a new line then refresh at the end. Just cleaning up a bit and your done.
Was This Post Helpful? 0
  • +
  • -

#9 xilith117  Icon User is online

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 13-August 11

Re: Brush Trouble

Posted 25 January 2013 - 06:25 AM

Ok, I can do those. Thanks. But how can I make my brush a circle instead of a + ? I don't understand my code very well, are pens + shaped? if so how do I go about repairing it? Thanks a billion HAWK.
Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1053
  • View blog
  • Posts: 4,080
  • Joined: 02-July 08

Re: Brush Trouble

Posted 25 January 2013 - 08:01 AM

Pens are pens not shapes. DrawLine uses the pen and draws a line by the points you give it. DrawCurve uses an array of points ans makes a curved line. Review your points you are assigning. Debug your work to see what it's doing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1