C# GDI+ technology
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 |
|
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 |
|
Sample code for drawing a straight line:
1 2 3 4 5 6 |
|
Draw a rectangle
The DrawRectangle method of the Graphics class, which can be overloaded. (1) Draw the rectangle specified by the Rectangle structure.
1 |
|
pen: Pen object, determines line color, width and style. rect: Represents the Rectangle structure to draw a rectangle. For example:
1 |
|
(2) Draw a rectangle specified by the coordinate pair, width and height.
1 |
|
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 |
|
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 |
|
(2) Draw an ellipse defined by a border (the border is specified by a pair of coordinates, height and width).
1 |
|
Sample code for drawing an ellipse:
1 2 3 4 5 6 7 |
|
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 |
|
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 |
|
Example code for drawing arc:
1 2 3 4 5 6 7 |
|
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 |
|
(2) Draw a polygon defined by a set of PointF structures.
1 |
|
Example code for drawing polygons:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
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 |
|
(2) Using the specified tension, draw a cardinal spline passing through a set of specified Point structures.
1 |
|
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 |
|
offset:从points参数数组中的第一个元素到曲线中起始点的偏移量。numberOfSegments:起始点之后要包含在曲线中的段数。 (4)使用指定张力,绘制经过一组指定Point结构的基数样条。
1 |
|
绘制基数样条示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
绘制贝赛尔样条
贝塞尔样条是由4个点指定的曲线:两个端点(p1,p2)和两个控制点(c1,c2)。曲线开始于p1,结束于p2。曲线不经过控制点,但是控制点像磁铁一样,在某些方向上拉伸曲线并影响曲线弯曲的方式。 调用Graphics类的DrawBezier方法,可重载。 (1)绘制由4个Point结构定义的贝塞尔样条。
1 |
|
4个Point点分别表示起始点、第一个控制点、第二个控制点和结束点。
(2)绘制由4个表示点的有序坐标对定义的贝塞尔样条。
1 |
|
x2,y2及x3,y3分别表示第1个、第2个控制点相应坐标。顺序和第一种方法类似。
绘制贝塞尔样条示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
绘制图形路径
路径是通过组合直线、矩形和简单的曲线而形成的。在GDI+中,GraphicsPath对象允许将基本构造块收集到一个单元中,调用一次Graphics类的DrawPath方法,就可以绘制出整个单元的直线、矩形、多边形和曲线。
1 |
|
pen:Pen对象,确定线条颜色、宽度和样式。path:要绘制的GraphicsPath图形路径。 PS:注意要引用System.Drawing.Drawing2D命名空间。
绘制图形路径示例代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
更多C# GDI+技术相关文章请关注PHP中文网!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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.

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 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.

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.

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.

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().
