C # GDI + technology GDI + overview GDI + is the successor of GDI (Graphics Device Interface included in earlier Windows versions. It is an application programming interface (API) that forms a subsystem of the Windows XP operating system ). The main namespace and description of the GDI + base class: system. drawingsystem. Drawing. drawing2dsystem. Drawing. Imaging
System. Drawing. Printing
System. Drawing. Design
System. Drawing. Text
The Basic Graph Drawing Graphics class is the core of GDI +. Graphics objects represent the GDI + drawing surface and provide a method for drawing objects to display devices. Graphics class encapsulates the methods for drawing straight lines, curves, graphs, images, and texts. It is a class for drawing straight lines, curves, graphs, images, and texts using GDI +, is the Basic class for all GDI + operations. The drawline method in the linear graphics class can be reloaded. It is mainly used to draw a line connecting two points specified by coordinate pairs. (1) draw a line connecting two point structures.
public void DrawLine(Pen pen, Point pt1,Point pt2)
Pen: Specifies the color, width, and style of the line. 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 the two points specified by the coordinate pair.
Public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)
Sample Code for creating 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); }The drawrectangle method used to draw the graphics class of a rectangle, which can be reloaded. (1) Draw a rectangle specified by the rectangle structure.
public void DrawRectangle(Pen pen,Rectangle rect)
Pen: Specifies the color, width, and style of the line. Rect: indicates the rectangle structure of the rectangle to be drawn. For example:
Rectangle rect = new Rectangle(0, 0, 80, 50);
(2) Draw a rectangle specified by coordinate pairs, width, and height.
public void DrawRectangle(Pen pen, int x, int y, int width, int height)
Pen: Specifies the color, width, and style of the line.
X: X coordinate of the upper left corner of the rectangle to be drawn. Y: Y coordinate in the upper left corner of the rectangle. Width and height indicate the 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); }The drawellipse method in the elliptical graphics class can be reloaded. It is mainly used to draw an ellipse specified by the rectangle structure. (1) draw an elliptic boundary specified by the rectangle structure.
public void DrawEllipse(Pen pen, Rectangle rect)
(2) draw an ellipse defined by the border (the border is specified by a coordinate, 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); }You can reload the drawarc method in the graphics class. (1) draw an arc that represents a part of the ellipse specified by the rectangle structure.
public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle)
Pen: Specifies the color, width, and style of the line.
Rect: rectangle structure, which defines the elliptical boundary. Startangle: the angle measured clockwise from the X axis to the starting point of the arc (in degrees ).
Sweepangle: the angle measured clockwise from the startangle parameter to the end point of the arc (in degrees ).
(2) draw an arc that represents an elliptical part specified by a coordinate, width, and height.
public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)
Example code for creating an 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); }The graphics object, pen object, and point (or pointf) object array are required to draw a polygon. The graphics class provides the drawpolygon method. The pen object storage is used to display the line attributes of a polygon, such as width and color. The point (or pointf) object array stores each vertex of a polygon. Reload allowed. (1) Draw a polygon defined by a group of point structures.
public void DrawPolygon(Pen pen, Point[] pints)
C # GDI + Technology