C# GDI+ technology

高洛峰
高洛峰Original
2016-12-17 10:01:341412browse

GDI+ Overview

GDI+ is the successor to GDI, the Graphics Device Interface included in earlier versions of Windows. It is an application programming interface (API) that forms a subsystem of the Windows XP operating system. The main namespaces and descriptions of GDI+ base classes: System.Drawing--Contains most classes, structures, enumerations and delegates related to basic drawing functions. System.Drawing.Drawing2D - Provides support for most advanced 2D and vector drawing operations, including anti-aliasing, geometry transformations, and graphics paths. System.Drawing.Imaging--Various classes that help with processing images (bitmaps, GIF files, etc.). System.Drawing.Printing--A class used when using a printer or print preview window as an output device. System.Drawing.Design--A number of predefined dialog boxes, property sheets, and other user interface elements related to extending the user interface during design. System.Drawing.Text – Class that performs more advanced operations on fonts and font families.

Basic graphics drawing

The Graphics class is the core of GDI+. The Graphics object represents the GDI+ drawing surface and provides methods for drawing objects to display devices. The Graphics class encapsulates the methods for drawing straight lines, curves, graphics, images and text. It is the class used by GDI+ to draw straight lines, curves, graphics, images and text. It is the basic class for all GDI+ operations.

Draw a straight line

The DrawLine method in the Graphics class can be overloaded and is mainly used to draw a line connecting two points specified by a coordinate pair. (1) Draw a line connecting two Point structures.

public void DrawLine(Pen pen, Point pt1,Point pt2)

pen: Pen object, determines line color, width and style. pt1:Point structure, indicating the first point to be connected. pt2:Point structure, indicating the second point to be connected. (2) Draw a line connecting two points specified by a coordinate pair.

Public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)

Sample code for drawing a straight line:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 2);
    graphics.DrawLine(myPen, 50, 30, 170, 30);
}

Draw a rectangle

The DrawRectangle method of the Graphics class, which can be overloaded. (1) Draw the rectangle specified by the Rectangle structure.

public void DrawRectangle(Pen pen,Rectangle rect)

pen: Pen object, determines line color, width and style. rect: Represents the Rectangle structure to draw a rectangle. For example:

Rectangle rect = new Rectangle(0, 0, 80, 50);

(2) Draw a rectangle specified by the coordinate pair, width and height.

public void DrawRectangle(Pen pen, int x, int y, int width, int height)

pen: Pen object, determines line color, width and style. x: The x-coordinate of the upper left corner of the rectangle to be drawn. y: The y coordinate of the upper left corner of the rectangle to be drawn. width and height represent width and height respectively. Sample code for drawing a rectangle:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 2);
    graphics.DrawRectangle(myPen, 70, 20, 80, 50);
}

Draw an ellipse

DrawEllipse method in the Graphics class, which can be overloaded. Mainly used to draw ellipses with boundaries specified by the Rectangle structure. (1) Draw an ellipse with a boundary specified by the Rectangle structure.

public void DrawEllipse(Pen pen, Rectangle rect)

(2) Draw an ellipse defined by a border (the border is specified by a pair of coordinates, height and width).

public void DrawEllipse(Pen pen, int x, int y, int width, int height)

Sample code for drawing an ellipse:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 3);
    Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
    graphics.DrawEllipse(myPen, myRectangle);
}

Drawing an arc

The DrawArc method in the Graphics class can be overloaded. (1) Draw an arc, which represents a part of the ellipse specified by the Rectangle structure.

public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle)

pen: Pen object, determines line color, width and style. rect: Rectangle structure, defining the boundary of the ellipse. startAngle: The angle (in degrees) measured clockwise from the x-axis to the starting point of the arc. sweepAngle: The angle (in degrees) measured clockwise from the startAngle parameter to the end point of the arc. (2) Draw an arc that represents the portion of the ellipse specified by a pair of coordinates, width and height.

public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)

Example code for drawing arc:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Blue, 5);
    Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
    graphics.DrawArc(myPen, myRectangle,210,120);
}

Drawing polygon

requires Graphics object, Pen object and Point (or PointF) object array. The Graphics class provides the DrawPolygon method. The Pen object stores the line attributes used to render polygons, such as width and color, etc. The Point (or PointF) object array stores the individual vertices of the polygon. Can be reloaded. (1) Draw a polygon defined by a set of Point structures.

public void DrawPolygon(Pen pen, Point[] pints)

(2) Draw a polygon defined by a set of PointF structures.

public void DrawPolygon(Pen pen, PointF[] pints)

Example code for drawing polygons:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    Point point1 = new Point(80, 20);
    Point point2 = new Point(40, 50);
    Point point3 = new Point(80, 80);
    Point point4 = new Point(160, 80);
    Point point5 = new Point(200, 50);
    Point point6 = new Point(160, 20);
    Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
    graphics.DrawPolygon(myPen, myPoints);
}

Drawing a cardinal spline

A cardinal spline is a series of individual curves that are connected to form a larger curve. Specified by an array of points and a tension parameter, the spline passes smoothly through each point of the array, without sharp corners or sudden changes in the steepness of the curve. (1) Draw a cardinal spline passing through a set of specified Point structures.

public void DrawCurve(Pen pen, Point[] points)

(2) Using the specified tension, draw a cardinal spline passing through a set of specified Point structures.

public void DrawCurve(Pen pen, Point[] points, float tension)

tension: A value greater than or equal to 0.0F, specifying the tension of the curve. (3) Starting at an offset relative to the start of the array, draw a cardinal spline through a set of specified PointF structures.

public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments)

offset:从points参数数组中的第一个元素到曲线中起始点的偏移量。numberOfSegments:起始点之后要包含在曲线中的段数。 (4)使用指定张力,绘制经过一组指定Point结构的基数样条。

public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments, float tension)

   

绘制基数样条示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    Point point1 = new Point(50, 20);
    Point point2 = new Point(60, 30);
    Point point3 = new Point(70, 25);
    Point point4 = new Point(100, 50);
    Point point5 = new Point(130, 30);
    Point point6 = new Point(150, 45);
    Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
    graphics.DrawCurve(myPen, myPoints, 1.0F);
}

   

绘制贝赛尔样条

贝塞尔样条是由4个点指定的曲线:两个端点(p1,p2)和两个控制点(c1,c2)。曲线开始于p1,结束于p2。曲线不经过控制点,但是控制点像磁铁一样,在某些方向上拉伸曲线并影响曲线弯曲的方式。 调用Graphics类的DrawBezier方法,可重载。 (1)绘制由4个Point结构定义的贝塞尔样条。

public void DrawBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4)

   

4个Point点分别表示起始点、第一个控制点、第二个控制点和结束点。
(2)绘制由4个表示点的有序坐标对定义的贝塞尔样条。

public void DrawBezier(Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)

   

x2,y2及x3,y3分别表示第1个、第2个控制点相应坐标。顺序和第一种方法类似。
绘制贝塞尔样条示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    Pen myPen = new Pen(Color.Red, 5);
    float startX = 50.0F;
    float startY = 80.0F;
    float controlX1 = 150.0F;
    float controlY1 = 20.0F;
    float controlX2 = 230.0F;
    float controlY2 = 50.0F;
    float endX = 190.0F;
    float endY = 80.0F;
    graphics.DrawBezier(myPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);
}

   

绘制图形路径

路径是通过组合直线、矩形和简单的曲线而形成的。在GDI+中,GraphicsPath对象允许将基本构造块收集到一个单元中,调用一次Graphics类的DrawPath方法,就可以绘制出整个单元的直线、矩形、多边形和曲线。

public void DrawPath(Pen pen, GraphicsPath path)

   

pen:Pen对象,确定线条颜色、宽度和样式。path:要绘制的GraphicsPath图形路径。 PS:注意要引用System.Drawing.Drawing2D命名空间。
绘制图形路径示例代码:

private void button1_Click(object sender, EventArgs e)
{
    Graphics graphics = this.CreateGraphics();
    GraphicsPath myGraphicsPath = new GraphicsPath();
    Pen myPen = new Pen(Color.Blue, 1);
    Point[] myPoints = { new Point(15, 30), new Point(30, 40), new Point(50, 30) };
    myGraphicsPath.AddArc(15, 20, 80, 50, 210, 120);
    myGraphicsPath.StartFigure();
    myGraphicsPath.AddCurve(myPoints);
    myGraphicsPath.AddString("图形路径", new FontFamily("华文行楷"), (int)FontStyle.Underline, 50, new PointF(20, 50), new StringFormat());
    myGraphicsPath.AddPie(180,20,80,50,210,120);
    graphics.DrawPath(myPen, myGraphicsPath);
}

   


更多C# GDI+技术相关文章请关注PHP中文网!

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