search

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.

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

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.

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

Sample code for drawing 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);
}

Draw a rectangle

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

public void DrawRectangle(Pen pen,Rectangle rect)

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

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

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

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

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:

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

public void DrawEllipse(Pen pen, Rectangle rect)

(2) Draw an ellipse defined by a border (the border is specified by a pair of coordinates, 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);
}

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.

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

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.

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

Example code for drawing 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);
}

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.

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

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

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

Example code for drawing polygons:

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

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.

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

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

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

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.

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

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

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

   

绘制基数样条示例代码:

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

   

绘制贝赛尔样条

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

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

   

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

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

   

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

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

   

绘制图形路径

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

public void DrawPath(Pen pen, GraphicsPath path)

   

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

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

   


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

Statement
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
From Web to Desktop: The Versatility of C# .NETFrom Web to Desktop: The Versatility of C# .NETApr 15, 2025 am 12:07 AM

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C# .NET and the Future: Adapting to New TechnologiesC# .NET and the Future: Adapting to New TechnologiesApr 14, 2025 am 12:06 AM

C# and .NET adapt to the needs of emerging technologies through continuous updates and optimizations. 1) C# 9.0 and .NET5 introduce record type and performance optimization. 2) .NETCore enhances cloud native and containerized support. 3) ASP.NETCore integrates with modern web technologies. 4) ML.NET supports machine learning and artificial intelligence. 5) Asynchronous programming and best practices improve performance.

Is C# .NET Right for You? Evaluating its ApplicabilityIs C# .NET Right for You? Evaluating its ApplicabilityApr 13, 2025 am 12:03 AM

C#.NETissuitableforenterprise-levelapplicationswithintheMicrosoftecosystemduetoitsstrongtyping,richlibraries,androbustperformance.However,itmaynotbeidealforcross-platformdevelopmentorwhenrawspeediscritical,wherelanguageslikeRustorGomightbepreferable.

C# Code within .NET: Exploring the Programming ProcessC# Code within .NET: Exploring the Programming ProcessApr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

C# .NET: Exploring Core Concepts and Programming FundamentalsC# .NET: Exploring Core Concepts and Programming FundamentalsApr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

Testing C# .NET Applications: Unit, Integration, and End-to-End TestingTesting C# .NET Applications: Unit, Integration, and End-to-End TestingApr 09, 2025 am 12:04 AM

Testing strategies for C#.NET applications include unit testing, integration testing, and end-to-end testing. 1. Unit testing ensures that the minimum unit of the code works independently, using the MSTest, NUnit or xUnit framework. 2. Integrated tests verify the functions of multiple units combined, commonly used simulated data and external services. 3. End-to-end testing simulates the user's complete operation process, and Selenium is usually used for automated testing.

Advanced C# .NET Tutorial: Ace Your Next Senior Developer InterviewAdvanced C# .NET Tutorial: Ace Your Next Senior Developer InterviewApr 08, 2025 am 12:06 AM

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C# .NET Interview Questions & Answers: Level Up Your ExpertiseC# .NET Interview Questions & Answers: Level Up Your ExpertiseApr 07, 2025 am 12:01 AM

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool