Home > Article > Backend Development > C# GDI+ Programming (2)
Commonly used drawing functions
DrawArc draws an arc
Example: graphics.DrawArc(pen,0,0,200,200,90,120)
The penultimate parameter represents the starting degree, and the last parameter is the spanned degree of the arc . For example, the starting degree is 90 and the arc spanning degree is 120
The red one is the arc. Similar methods include DrawPie to draw a fan shape and FillPie to fill a fan shape. All have starting degrees and spanning degrees.
DrawPolygon draws polygons
Example: Point []pt=new Point[]{new Point(0,50),new Point(0,100),new Point(100,100)};
graphics.DrawPolygon(pen, pt);
Point array specifies the position of each point and the number of points. The polygon drawn by DrawPolygon is closed, and DrawPolygon will automatically connect the first point and the last point.
DrawCurve draws cardinal spline
Example: Point[] pt = new Point[] { new Point(0, 0), new Point(100, 50),new Point(200,0)}; (pen, pt, 1.5f);
{
graphics = e.Graphics;
Pen pen = new Pen(Col or.FromArgb(0, 255, 0), 2);
Rectangle RECT = New Rectangle (10, 10, 100, 100);
GraphicSpath GRCPATH = New GraphicsPath ();
Grcpath.addrectangle (RECT); Add a string path Fontfamily Famfont = newFontFamily("Hellbody");
FontFamily, grcPath);
}
PathGradientBrush
Usage example:
V Private Void Formpaaint (Object SENDER, PAINTEVENTARGS E)
{
// Create paths
GraphicSpath Path = New GraphicSpath (); , 100); ;
PathGradientBrush brush //The color on the path (point) is black
brush. SurroundColors = new Color[] {Color.Black};
. The color on is black, that is to say, from the center point to each point on the path, there is a white to black gradient.
You can also specify the center point yourself. If you don’t want to use the center point calculated by PathGradientBrush, just specify CenterPoint, such as brush.CenterPoint = new Point(20, 50);
Path brush with multiple color gradients
Multiple colors Gradient has been introduced in the previous linear gradient painting, so the multiple color gradients of the path are also similar. Let’s look at the example directly:
Rectangle rect = new Rectangle(0, 0, 100, 100);
path.AddRectangle(rect);//Create a path brush
PathGradientBrush brush = new PathGradientBrush(path);ColorBlend Red,Color.Green,Color.White };
//Fill a rectangle with the path brush
e.Graphics.FillRectangle(brush, rect);}
} to Make straight lines, and then two-thirds of this line is a gradient from a certain color to a certain color, and one-third is a gradient from a certain color to a certain color.
The above is the gradient range from red to green: 0~2/3, green to white (center point color): 2/3~1; Assume that the length of the entire straight line is replaced by 1. In addition, the center point position can also be customized.
Brush with a path composed of points
Example:
private void formPaint(Object sender, PaintEventArgs e)
{
Rectangle rect = new Rectangle(0, 0, 100, 100);
Point []pts=new Point[] {new Point(50,0),new Point (0,100),new Point(100,100)};
PathGradientBrush brush=new PathGradientBrush(pts); // Colors on path points
brush.SurroundColors=new Color[]{Color.Black};
e.Graphics.FillRectangle(brush, rect);
}
This kind of graphics composed of points is created directly by the path brush, without passing GraphicsPath, you can also specify three For the above points, the path brush will automatically connect these points (in order) to form a shape, and then fill it in, but the filling range is limited to the shape formed by these points. Just like above, this brush is used to fill a rectangle,
{
//Create path path = new GraphicsPath() ;
Rectangle rect = new Rectangle(0, 0, 100, 100);
Point[] pts = new Point[] { new Point(50, 0), new Point(0, 100), new Point(100, 100)};
path.AddLines(pts);
PathGradientBrush brush = new PathGradientBrush( path);
brush. e.Graphics.FillPath(brush , path);
}
The FillPath function is just like DrawPath, but DrawPath uses a brush to describe the path, while FillPath uses "fill" to describe the path.
Note that the path filled by FillPath has certain restrictions and is closed. Don't get into conflict.
The SurroundColors attribute member in the PathGradientBrush class, multiple colors on the path points
Before, I only specified one color, SurroundColors is a Color array, then it can specify multiple colors. What is specified is the color of the corner points of a figure. For example, a triangle has three corners. You can specify three different colors for these three corner points, but it cannot be four colors, because a triangle only has three corners. If it exceeds the range, an error will occur.
The same goes for rectangles, you can specify four colors, but if the number of specified colors is less than the number of corner points, such as a rectangle, I only specify the color of one corner point.
Then the remaining corner points use the last color value of the SurroundColors array.
Take the first example of the path brush. Let’s modify it and specify the colors of the four corner points. As follows:
V Private void Formpaaint (Object SENDER, PAINTEVENTARGS E) { // Create paths
GraphicSpath Path = New GraphicSpath (); , 100); ;
PathGradientBrush brush //Specify the colors of different corner points
brush.SurroundColors = new Color[] { Color.Black,Color.Red,Color.Green,Color.Blue };
‐ ‐ ‐ ‐ ‐ ‐ ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐ ‐ You can roughly see the difference in color at the dots, which are black, red, blue, and green. Corresponding to the four corner points: upper left (0,0), upper right (100, 0), lower right (100, 100), and lower left (0, 100).
Let’s talk about how this gradient brush looks, or what it’s based on. First, the corner point (0, 0) is designated as Color.Black in SurroundColos, and the corner point (100, 0) is Color.Red, and these two points can be connected into a straight line. This straight line also has a gradient, from black to red. And this straight line is on the path.
Then each point on this path (straight line) will have a different color.
For example, (0, 0) is black, (1, 0) is a lighter black. . . . . (99,0) is dark red (100,0) is red.
The color of the center point is white, so the straight line from the center point to 0,0 is a gradient from white to black, and to (1,0) is a gradient from white to lighter black.
The same goes for the gradients on other corner points.
The gradient above is formed according to these rules.
Another triangle example:
private void formPaint(Object sender, PaintEventArgs e)
{//Create path
Path = new GraphicsPath( );Point []pts = new Point[] { new Point( 50, 0), new Point(0, 100), new Point(100, 100) };
path.AddLines(pts); PathGradientBrush brush=new PathGradientBrush(path); //Center color whitebrush .CenterColor=Color.White;
// Corner point color: red, green, blue.按pts数组顺序依次brush.SurroundColors = new Color[] { Color.Red, Color.Green, Color.Blue };
//用路径画刷填充路径e.Graphics.FillPath(brush, path);
}After understanding the above knowledge points, you can try to make an example of a five-pointed star shape (use a path gradient brush and use different corner colors).
For more C# GDI+ Programming (2) related articles, please pay attention to the PHP Chinese website!