Yeah... it's been a while I posted anything into this blog. I've been doing a lot
of different things on CF (one of which is SDF on
OpenNETCF.org) which I am going to tell you about later on...
Meanwhile, as an incentive to the loyal and new readers I am posting the function
for drawing gradient colors in CF. As you know GradientBrush is not supported in CF
and one of my recent projects required filling a background with a gradient colors,
so I've put together the following function:
public static void DrawGradientVertical(Graphics g,
Color color1, Color color2, Rectangle rect, int percent)
{
Pen pen = null;
//calculate half color
Color halfColor = Color.FromArgb((color1.R + color2.R) / 2, (color1.G
+ color2.G) / 2, (color1.B + color2.B) / 2);
int R_color = color1.R;
int G_color = color1.G;
int B_color = color1.B;
int R_step = 0;
int G_step = 0;
int B_step = 0;
int sep1 = rect.Height / 2;
int sep2 = rect.Height / 2;
//calculate steps for changing between
color1 and halfColor
if (rect.Height > 0) //avoid 0 division
{
sep1 = rect.Height * percent / 100;
R_step = (halfColor.R - color1.R) / sep1;
G_step = (halfColor.G - color1.G) / sep1;
B_step = (halfColor.B - color1.B) / sep1;
}
//draw the lines
for(int i=0;i<sep1;i++)
{
pen = new Pen(Color.FromArgb(R_color,G_color,B_color));
g.DrawLine(pen, rect.X, rect.Top + i, rect.Right, rect.Top
+ i);
R_color = R_color+R_step;
G_color = G_color+G_step;
B_color = B_color+B_step;
}
//calculate steps for changing between
halfColor and color2
if (rect.Height > 0) //avoid 0 division
{
sep2 = rect.Height - sep1;
R_step = (color2.R - halfColor.R) / sep2;
G_step = (color2.G - halfColor.G) / sep2;
B_step = (color2.B - halfColor.B) / sep2;
}
R_color = halfColor.R;
G_color = halfColor.G;
B_color = halfColor.B;
//draw the lines
for(int i=sep1;i<rect.Height;i++)
{
pen = new Pen(Color.FromArgb(R_color,G_color,B_color));
g.DrawLine(pen, rect.X, rect.Top + i, rect.Right, rect.Top
+ i);
R_color = R_color+R_step;
G_color = G_color+G_step;
B_color = B_color+B_step;
}
}
This function should fill the given rectangle with the gradient colors vertically.
You can easily change it to make a filling in the horizontal direction.