Draw a line with linear gradient

mcerk

Well-known member
Joined
Nov 1, 2004
Messages
78
Hi. How can I draw a line with linear gradient. Lets say to start at color.Red and end with color.Blue


tx,

matej
 
Look into the System.Drawing.Drawing2D.LinearGradientbrush class. Here is an example:
C#:
Graphics gr = this.CreateGraphics();
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(0,0,100,100), Color.NavajoWhite, Color.Red, LinearGradientMode.Vertical);
gr.FillRectangle(br, br.Rectangle);
gr.Dispose();
 
Yes, I believed that drawing rectangle is maybe my only choice. But I have to draw a line. So I could draw very thin rectangle - Ill have some trouble with rotation, but I can firuge it out. brrrrrr....


Ill have to take a pen and paper into my hands and do some mathematics...



mutant said:
Look into the System.Drawing.Drawing2D.LinearGradientbrush class. Here is an example:
C#:
Graphics gr = this.CreateGraphics();
LinearGradientBrush br = new LinearGradientBrush(new Rectangle(0,0,100,100), Color.NavajoWhite, Color.Red, LinearGradientMode.Vertical);
gr.FillRectangle(br, br.Rectangle);
gr.Dispose();
 
Oh, im sorry, for some reason I didnt think about you needing a line when i wrote the code :D. You can create a Pen object used to draw the line from the brush so that is not a problem.
C#:
Graphics gr = this.CreateGraphics();

LinearGradientBrush br = new LinearGradientBrush(new Rectangle(0,0,100,100), Color.NavajoWhite, Color.Red, LinearGradientMode.Vertical);
Pen lpen = new Pen(br, 1);

gr.DrawLine(lpen,0,0,100,200);

gr.Dispose();
 
Back
Top