The System.Drawing.Drawing2D namespace provides advanced two-dimensional and
vector graphics functionality. The following table lists some of the important
classes in theSystem.Drawing.Drawing2D namespace, and their descriptions.
CLASS |
DESCRIPTION |
PathGradientBrush |
Encapsulates a Brush object that fills
the interior of a GraphicsPath object with a gradient. This class cannot
be inherited. |
LinearGradientBrush |
Encapsulates a Brush with a linear
gradient. This class cannot be inherited. |
ColorBlend |
Defines arrays of colors and positions
used for interpolating color blending in a multicolor gradient. This
class cannot be inherited. |
GraphicsPath |
Represents a series of connected lines
and curves. This class cannot be inherited. |
HatchBrush |
Defines a rectangular brush with a
hatch style, a foreground color, and a background color. This class
cannot be inherited. |
Blend |
Defines a blend pattern for a
LinearGradientBrush object. This class cannot be inherited. |
PathData |
Contains the graphical data that makes
up a GraphicsPath object. This class cannot be inherited. |
In the following example the
System.Drawing.Drawing2D namespace is implemented, with which a star with
different effects like rotation and change of color with the aid of timer is
drawn.
First we are going to implement the System .Drawing.Drawing2D namespace.
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
System.Drawing.Drawing2D; <-//this is the necessary namespace
using
System.Threading;
Then, we are going to declare the variables that are going to be used in the
program.
double
a = 0.0;
double
angle;
Random ram =
new Random();//this
object is going to be used to the color
bool
alto = false;
In the Form's event resize we are going to use the method Invalidate(), this
method invalidates the entire surface of the control and causes the control to
be redrawn.
private
void Form1_Resize(object
sender, EventArgs e)
{
Invalidate();
this.Refresh();
}
Inside of timer´s event we will implement the code of the picture,we will use
the Point structure,this represents an x- and y-coordinate pair in
two-dimensional space, and we can found the color and the angle for the figure´s
rotation too.
private
void timer1_Tick(object
sender, EventArgs e)
{
Point[] apt =
new Point[5];
for (int i =
0; i < apt.Length; i++)
{
angle = ((i * 0.8 - 0.5) * Math.PI) +
a;
apt[i] = new
Point((int)(ClientSize.Width * (.5 + .48
* Math.Cos(angle))), (int)(ClientSize.Height
* (.5 + .48 * Math.Sin(angle))));
}
PathGradientBrush
brocha = new
PathGradientBrush(apt);
brocha.CenterColor = Color.Chocolate;
brocha.SurroundColors = new
Color[5] { Color.FromArgb(ram.Next(256),
ram.Next(256), ram.Next(256)), Color.FromArgb(ram.Next(256),
ram.Next(256), ram.Next(256)), Color.FromArgb(ram.Next(256),
ram.Next(256), ram.Next(256)), Color.FromArgb(ram.Next(256),
ram.Next(256), ram.Next(256)), Color.FromArgb(ram.Next(256),
ram.Next(256), ram.Next(256)) };
Graphics g =
this.CreateGraphics();
//
RECTANGLE WITH FILLER DEGRADED
Rectangle
rect = new Rectangle(0,
0, ClientSize.Width, ClientSize.Height);
LinearGradientBrush
lBrush = new
LinearGradientBrush(rect, Color.FromArgb(ram.Next(256),
ram.Next(256), ram.Next(256)), Color.FromArgb(ram.Next(256),
ram.Next(256), ram.Next(256)), LinearGradientMode.Vertical);
g.FillRectangle(lBrush, rect);
g.FillRectangle(brocha, 0, 0, ClientSize.Width, ClientSize.Height);
Thread.Sleep(200);
a += 45;
}
And finally, the button's code to enable and disable the timer.
private
void button1_Click(object
sender, EventArgs e)
{
if (alto ==
false)
{
timer1.Enabled = true;
alto = true;
}
else
{
alto = false;
timer1.Enabled = false;
}
}