Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I'm writing a kind of a paint program that works with lines.
Now when I try in the auto color system to implant a kind of "gradient", so the
first line will get the color 255;0;0 and the next line will get 255;1;0 and so
on... I have been stuck at a problem, if I set it to try and go from 255;0;0 to
255;255;255, it goes to 255;255;0 and no further and it won't go back if it
reached that.
Here's my code for it.

EDIT: Just so you know the picturebox2 contains the color for the
pen, and it's not one line that should be gradient. It's many different lines that
should be gradient with each other.

EDIT2 (for more clearance): if we say we have color1 and color2, and color1 =
255;0;0 and color2 = 255;0;255 then for every line i draw the color of the line be
more like color2 so that the first line has the color 255;0;0 the next line is
255;0;1 the third is 255;0;2 and so on

EDIT3: this is some updated code now.. this works but it's quite buggy, sometimes i goes to a completely random color and it should just go up or down :\

Edit4: here a little test with the code below http://img227.imageshack.us/img227/337/testobject.png

the problem is that it's quite buggy :(

VB
If cb5.Checked = True Then
                            Try
                                Dim c1 As Color = colo1.BackColor
                                Dim c2 As Color = colo2.BackColor
                                Dim pc1 As Color = PictureBox2.BackColor
                                If counter = 0 Then
                                    If pc1 = c2 Then
                                        counter = 1
                                    Else
                                        If pc1.R = c2.R Then
                                            If pc1.G = c2.G Then
                                                If pc1.B = c2.B Then
                                                    counter = 1
                                                Else
                                                    PictureBox2.BackColor = Color.FromArgb(pc1.R, pc1.G, pc1.B + 1)
                                                End If
                                            Else
                                                PictureBox2.BackColor = Color.FromArgb(pc1.R, pc1.G + 1, pc1.B)
                                            End If
                                        Else
                                            PictureBox2.BackColor = Color.FromArgb(pc1.R + 1, pc1.G, pc1.B)
                                        End If
                                    End If
                                ElseIf counter = 1 Then
                                    If pc1 = c1 Then
                                        counter = 0
                                    Else
                                        If pc1.B = c1.B Then
                                            If pc1.G = c1.G Then
                                                If pc1.R = c1.R Then
                                                    counter = 0
                                                Else
                                                    PictureBox2.BackColor = Color.FromArgb(pc1.R - 1, pc1.G, pc1.B)
                                                End If
                                            Else
                                                PictureBox2.BackColor = Color.FromArgb(pc1.R, pc1.G - 1, pc1.B)
                                            End If
                                        Else
                                            PictureBox2.BackColor = Color.FromArgb(pc1.R, pc1.G, pc1.B - 1)
                                        End If
                                    End If
                                End If
                            Catch
                            End Try
                            p = New Pen(PictureBox2.BackColor)
                            PictureBox1.Image = bit
                        End If


I hope I have described it well and I can get some help from you. :)
Posted
Updated 22-Apr-10 0:49am
v9

Pens can be constructed with gradient brushes. I think what you want would be more like this:
VB
Dim Rect As New Rectangle(0, 0, a, 1)

Using LGB As New LinearGradientBrush(Rect, Color1, Color2, LinearGradientMode.Horizontal)
    Using P As New Pen(LGB, 1)
        G.DrawLine(P, x, y, x + a, y + b)
    End Using
End Using

This creates a brush having a horizontal gradient from Color1 to Color2, uses it to construct a pen with a thickness of 1 pixel, then uses the pen to draw a line from (x, y) to (x+a, y+b). Rect represents the boundaries of the line itself, so you will need to size it according to whether the line is horizontal, vertical or diagonal.

Second, there is no need to store colors in controls. Why not simply use a few Color objects?
 
Share this answer
 
v3
Why on earth would anyone write a paint program that contains any pictureboxes ? That's just wrong before you start. GDI+ has gradient brushes built in.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900