C# GDI+ technology

Dec 17, 2016 am 10:01 AM

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.

1

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

Copy after login

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.

1

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

Copy after login

Sample code for drawing a straight line:

1

2

3

4

5

6

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

}

Copy after login

Draw a rectangle

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

1

public void DrawRectangle(Pen pen,Rectangle rect)

Copy after login

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

1

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

Copy after login

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

1

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

Copy after login

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:

1

2

3

4

5

6

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

}

Copy after login

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.

1

public void DrawEllipse(Pen pen, Rectangle rect)

Copy after login

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

1

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

Copy after login

Sample code for drawing an ellipse:

1

2

3

4

5

6

7

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

}

Copy after login

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.

1

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

Copy after login

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.

1

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

Copy after login

Example code for drawing arc:

1

2

3

4

5

6

7

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

}

Copy after login

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.

1

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

Copy after login

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

1

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

Copy after login

Example code for drawing polygons:

1

2

3

4

5

6

7

8

9

10

11

12

13

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

}

Copy after login

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.

1

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

Copy after login

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

1

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

Copy after login

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.

1

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

Copy after login

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

1

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

Copy after login

绘制基数样条示例代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

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

}

Copy after login

绘制贝赛尔样条

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

1

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

Copy after login

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

1

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

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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

}

Copy after login

绘制图形路径

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

1

public void DrawPath(Pen pen, GraphicsPath path)

Copy after login

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

1

2

3

4

5

6

7

8

9

10

11

12

13

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

}

Copy after login

   


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

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use various symbols in C language How to use various symbols in C language Apr 03, 2025 pm 04:48 PM

The usage methods of symbols in C language cover arithmetic, assignment, conditions, logic, bit operators, etc. Arithmetic operators are used for basic mathematical operations, assignment operators are used for assignment and addition, subtraction, multiplication and division assignment, condition operators are used for different operations according to conditions, logical operators are used for logical operations, bit operators are used for bit-level operations, and special constants are used to represent null pointers, end-of-file markers, and non-numeric values.

What is the role of char in C strings What is the role of char in C strings Apr 03, 2025 pm 03:15 PM

In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

How to handle special characters in C language How to handle special characters in C language Apr 03, 2025 pm 03:18 PM

In C language, special characters are processed through escape sequences, such as: \n represents line breaks. \t means tab character. Use escape sequences or character constants to represent special characters, such as char c = '\n'. Note that the backslash needs to be escaped twice. Different platforms and compilers may have different escape sequences, please consult the documentation.

The difference between char and wchar_t in C language The difference between char and wchar_t in C language Apr 03, 2025 pm 03:09 PM

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

The difference between multithreading and asynchronous c# The difference between multithreading and asynchronous c# Apr 03, 2025 pm 02:57 PM

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

How to convert char in C language How to convert char in C language Apr 03, 2025 pm 03:21 PM

In C language, char type conversion can be directly converted to another type by: casting: using casting characters. Automatic type conversion: When one type of data can accommodate another type of value, the compiler automatically converts it.

What is the function of C language sum? What is the function of C language sum? Apr 03, 2025 pm 02:21 PM

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

How to use char array in C language How to use char array in C language Apr 03, 2025 pm 03:24 PM

The char array stores character sequences in C language and is declared as char array_name[size]. The access element is passed through the subscript operator, and the element ends with the null terminator '\0', which represents the end point of the string. The C language provides a variety of string manipulation functions, such as strlen(), strcpy(), strcat() and strcmp().

See all articles