C # GDI + technology

Source: Internet
Author: User
Tags in degrees

GDI + Overview

GDI + is a 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. Main namespaces and descriptions for GDI + base classes: system.drawing--contains most of the classes, structs, enumerations, and delegates associated with basic drawing functionality. The system.drawing.drawing2d--provides support for most advanced 2D and vector drawing operations, including anti-aliasing, geometric transformations, and graphics paths. system.drawing.imaging--helps handle various classes of images (bitmaps and GIF files, etc.). system.drawing.printing--the class that the printer or Print preview window uses as the output device. system.drawing.design--Some pre-defined dialog boxes, property sheets, and other user interface elements related to extending the user interface during design time. system.drawing.text--classes that perform more advanced operations on fonts and font families.

Basic graphic Drawing

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

Draw a line

The DrawLine method in the Graphics class, which can be overloaded, is used primarily to draw a line that joins two points specified by a coordinate pair. (1) Draw a line that connects two point structures.

public void DrawLine (pen pen, point Pt1,point pt2)

The Pen:pen object that determines the line color, width, and style. A pt1:point structure that represents the first point to connect to. A pt2:point structure that represents the second point to connect to. (2) Draws a line that connects two points specified by a coordinate pair.

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

Sample code to draw a 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) Draws the rectangle specified by the rectangle structure.

public void DrawRectangle (Pen pen,rectangle rect)

The Pen:pen object that determines the line color, width, and style. Rect: Represents the rectangle structure to draw the rectangle. For example:

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

(2) Draws a rectangle specified by coordinate pairs, widths, and heights.

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

The Pen:pen object that determines the line color, width, and style. X: The x-coordinate of the upper-left corner of the rectangle to draw. Y: the y-coordinate of the upper-left corner of the rectangle to draw. Width and height represent widths and heights, respectively. Sample code for drawing rectangles:

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 Ellipse

The DrawEllipse method in the Graphics class, which can be overloaded. The ellipse that is used primarily to draw boundaries specified by the rectangle structure. (1) Draws the ellipse specified by the rectangle structure at the boundary.

public void DrawEllipse (pen pen, Rectangle Rect)

(2) draws an ellipse defined by the bounding rectangle, specified by a pair of coordinates, heights, and widths.

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

Sample code for drawing ellipses:

private void Button1_Click (object sender, EventArgs e) {    graphics graphics = this. CreateGraphics ();    Pen mypen = new Pen (color.blue, 3);    Rectangle myrectangle = new Rectangle (+,--);    Graphics. DrawEllipse (Mypen, myrectangle);}

Drawing arcs

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

public void DrawArc (pen pen, Rectangle rect, float startangle, float sweepAngle)

The Pen:pen object that determines the line color, width, and style. Rect:rectangle structure that defines the ellipse boundary. StartAngle: The angle measured in degrees 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) Draws an arc that represents the ellipse part specified by a pair of coordinates, widths, and heights.

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

To draw an instance code for 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 (+,--);    Graphics. DrawArc (Mypen, myrectangle,210,120);}

Drawing polygons

Requires a graphics object, a Pen object, and an array of point (or PointF) objects. The Graphics class provides the DrawPolygon method, which stores the line properties used to render polygons, such as width and color, and a point (or pointf) object array that stores the individual vertices of the polygon. can be overloaded. (1) Draws a polygon defined by a set of point structures.

public void DrawPolygon (pen pen, point[] pints)

(2) Draw polygons defined by a set of pointf structures.

public void DrawPolygon (pen pen, pointf[] pints)

To draw the Polygon sample code:

private void Button1_Click (object sender, EventArgs e) {    graphics graphics = this. CreateGraphics ();    Pen mypen = new Pen (color.red, 5);    Point point1 = new Point (+);    Point point2 = new Point (+);    Point point3 = new Point (+);    Point point4 = new Point (.);    Point point5 = new Point (.);    Point point6 = new Point (.);    Point[] mypoints = {point1, Point2, Point3, Point4, POINT5, point6};    Graphics. DrawPolygon (Mypen, mypoints);}

Draw Cardinal Spline

A cardinal spline is a series of separate curves that are joined together to form a larger curve. specified by the array of points and tension parameters, the spline passes through each point of the array smoothly, with no sharp and abrupt changes in the steepness of the curve. (1) Draws a cardinal spline that passes through a set of specified point structures.

public void DrawCurve (pen pen, point[] points)

(2) Draws a cardinal spline that passes through a set 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 that specifies the tension of the curve. (3) Draws a cardinal spline that passes through a set of specified pointf structures, starting at an offset relative to the beginning 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: The number of segments to include in the curve after the starting point. (4) Draw a cardinal spline that passes through a set of specified point structures using the specified tension.

public void DrawCurve (pen pen, point[] points, int offset, int numberofsegments, float tension)

To draw the cardinal spline sample code:

private void Button1_Click (object sender, EventArgs e) {    graphics graphics = this. CreateGraphics ();    Pen mypen = new Pen (color.red, 5);    Point point1 = new Point (a);    Point point2 = new Point (at);    Point point3 = new Point (+);    Point point4 = new Point (+);    Point point5 = new Point (.);    Point point6 = new Point (.);    Point[] mypoints = {point1, Point2, Point3, Point4, POINT5, point6};    Graphics. DrawCurve (Mypen, mypoints, 1.0F);}

To draw a Bezier spline

A Bezier spline is a curve specified by 4 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 stretches the curve in some direction and affects how the curve bends, like a magnet. Call the DrawBezier method of the graphics class, which can be overloaded. (1) Draw a Bezier spline defined by 4 point structures.

public void DrawBezier (pen pen, point pt1, point Pt2, point Pt3, point Pt4)

The 4 point points represent the starting point, the first control point, the second control point, and the end point, respectively.
(2) Draws a Bezier spline defined by an ordered coordinate pair of 4 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 and X3,y3 respectively represent the corresponding coordinates of the 1th and 2nd control points. The order is similar to the first method.
To draw the Bezier spline sample code:

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

Draw a graphics path

Paths are formed by combining lines, rectangles, and simple curves. In GDI +, the GraphicsPath object allows the basic building blocks to be collected into a single cell, calling the DrawPath method of a graphics class to draw lines, rectangles, polygons, and curves for the entire cell.

public void DrawPath (pen pen, GraphicsPath Path)

The Pen:pen object that determines the line color, width, and style. Path: The GraphicsPath graphics path to draw. PS: Note to refer to the System.Drawing.Drawing2D namespace.
Sample code to draw a graphics path:

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 (at (), new Point (+), new Point (+)};    Mygraphicspath.addarc (a) (a);    Mygraphicspath.startfigure ();    Mygraphicspath.addcurve (mypoints);    Mygraphicspath.addstring ("Graphics Path", New FontFamily ("XXFarEastFont-Xingkai"), (int) fontstyle.underline, new PointF (+), new StringFormat ());    Mygraphicspath.addpie (180,20,80,50,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.