Home  >  Article  >  Backend Development  >  C# GDI+ Programming (2)

C# GDI+ Programming (2)

高洛峰
高洛峰Original
2016-12-17 10:17:481390browse

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);

The last parameter represents the tension value. I don’t know much about this drawing function, I can only roughly know what it is. It can be said that it cannot be used freely.

There must be at least three points to form a curve. You can see a picture, which I copied from the GDI+ reference material.

DrawBezier draws Bezier splines

Example: graphics.DrawBezier(pen, 100, 0, 200, 20, 0, 80, 100, 100)

A Bezier spline is composed of four points. The first point and the last point act as two points of the straight line, while the other two points act as magnets. Although the line does not pass through these two magnet points,

but these two magnet points will move the line towards it. suck. Thus forming a Bessel spline.

Path GraphicsPath

GraphicsPath class attribute System.Drawing.Drawing2D namespace

Path is composed of various lines. Then, a rectangle can also be regarded as composed of four straight lines, and a circle can also be regarded as composed of Made up of several arcs.

So the GraphicsPath class has path functions for adding various shapes, such as AddLine linear path, AddEllipse elliptical path, AddRectangle rectangular path,

AddBezier Bezier path, AddString string path, etc.

These paths have been added, and of course they cannot be seen. We can use the DrawPath function in the Graphics class to describe the trajectory of the path with a brush.

Look at the example:

private void formPaint(Object sender, PaintEventArgs e)

{
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:


                                                                                                                                                                                              use using gradation to use ’ s ’s gravitate to radiate through using the same way in.      GraphicsPath path = new GraphicsPath();

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,

but the part beyond the triangle shape is not filled. This is the same thing as the graphics path, only limited to filling the path. This graphics creation can also be completed through GraphicsPath, such as the AddPolygon polygon path function. Then fill it with the FillPolygon function in the Graphics class.

You can also use AddLines to add a path function. This function is a graph composed of straight lines, but it is basically composed of points. Two points form a straight line!

However, the formed graphics must be closed, otherwise the desired result cannot be achieved. Then call FillPath to fill the path.

AddLines example:

private void formPaint(Object sender, PaintEventArgs e)

{

//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 white

          brush .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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# GDI+ Programming (3)Next article:C# GDI+ Programming (3)