C # GDI + Technology

Source: Internet
Author: User
Tags in degrees

C # GDI + Technology
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. Drawing -- contains most classes, structures, enumeration, and delegation related to the basic Drawing function. System. Drawing. Drawing2D -- supports most advanced 2D and Vector Plotting operations, including eliminating sertices, geometric conversions, and graphic paths. System. Drawing. Imaging-helps process various types of images (such as bitmap and GIF files. System. Drawing. Printing -- specifies the printer or print preview window as the class used to output the device. System. Drawing. Design-predefined dialog boxes, attribute tables, and other user interface elements are related to extending the user interface during Design. System. Drawing. Text-class that performs more advanced operations on the font and font family. 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)
(2) Draw a polygon defined by a group of PointF structures.
public void DrawPolygon(Pen pen, PointF[] pints)
Code example for drawing a polygon:
        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);        }
A base spline is a series of separate curves that are connected to form a large curve. Specified by the vertex array and tension parameter, the spline smoothly passes through each vertex of the array, and there is no sharp angle or sudden change in the steep degree of the curve. (1) Draw a base spline that passes through a group of specified Point structures.
public void DrawCurve(Pen pen, Point[] points)
(2) Draw a base spline that passes through a group of specified Point structures using the specified tension.
public void DrawCurve(Pen pen, Point[] points, float tension)
Tension: a value greater than or equal to 0.0F. It specifies the tension of the curve. (3) Draw a base spline that passes through a group of specified PointF structures from the offset relative to the starting position of the array.
public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments)
Offset: the offset from the first element in the points parameter array to the starting point in the curve. NumberOfSegments: number of segments to be included in the curve after the start point. (4) Draw a base spline that passes through a group of specified Point structures using the specified tension.
public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments, float tension)
Sample Code for drawing a base spline:
        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);        }
Drawing the besell spline is a curve specified by four points: two endpoints (p1, p2) and two control points (c1, c2 ). The curve starts at p1 and ends at p2. The curve does not pass through the control point, but the control point is like a magnet. In some directions, the curve is stretched and the bending method of the curve is affected. Call the drawbezr method of the Graphics class, which can be reloaded. (1) Draw the Bessel spline defined by four Point structures.
public void DrawBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4)
The four Point points indicate the starting Point, the first control Point, and the second control Point.
(2) Draw the besell spline defined by four pairs of ordered coordinates representing points.
public void DrawBezier(Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
X2, y2, x3, and y3 represent the coordinates of 1st and 2nd control points, respectively. The sequence is similar to the first method.
Sample Code for drawing the besell spline:
        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);        }
The drawing Path is formed by combining straight lines, rectangles, and simple curves. In GDI +, the GraphicsPath object allows the collection of basic construction blocks into a unit and calls the DrawPath method of the Graphics class once, you can draw the line, rectangle, polygon, and curve of the entire unit.
public void DrawPath(Pen pen, GraphicsPath path)
Pen: Specifies the color, width, and style of the line. Path: The GraphicsPath graph path to be drawn. PS: You must reference the System. Drawing. Drawing2D namespace.
Sample Code for drawing a graphic path:
Private void button#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 ("graphic path", new FontFamily (" 文 "), (int) FontStyle. underline, 50, new PointF (20, 50), new StringFormat (); myGraphicsPath. addPie (210,120,); graphics. drawPath (myPen, myGraphicsPath );}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.