C# GDI+技术
GDI+概述
GDI+是GDI(即Windows早期版本中附带的Graphics Device Interface)的后继者。它是一种构成Windows XP操作系统的子系统的应用程序编程接口(API)。 GDI+基类的主要命名空间及说明: System.Drawing--包含与基本绘图功能相关的大多数类、结构、枚举和委托。System.Drawing.Drawing2D--为大多数高级2D和矢量绘图操作提供了支持,包括消除锯齿、几何转换和图形路径。System.Drawing.Imaging--帮助处理图像(位图和GIF文件等)的各种类。System.Drawing.Printing--把打印机或打印预览窗口作为输出设备时使用的类。System.Drawing.Design--一些预定义的对话框、属性表和其他用户界面元素,与在设计期间扩展用户界面相关。System.Drawing.Text--对字体和字体系列执行更高级操作的类。
基本图形绘制
Graphics类是GDI+的核心,Graphics对象表示GDI+绘图表面,提供了对象绘制到显示设备的方法。Graphics类封装了绘制直线、曲线、图形、图像和文本的方法,是GDI+实现绘制直线、曲线、图形、图像和文本的类,是进行一切GDI+操作的基础类。
绘制直线
Graphics类中的DrawLine方法,可重载,主要用来绘制一条连接由坐标对指定的两个点的线条。 (1)绘制一条连接两个Point结构的线。
public void DrawLine(Pen pen, Point pt1,Point pt2)
pen:Pen对象,确定线条颜色、宽度和样式。pt1:Point结构,表示要连接的第一个点。pt2:Point结构,表示要连接的第二个点。 (2)绘制一条连接由坐标对指定的两个点的线条。
Public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)
绘制直线的示例代码:
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); }
绘制矩形
Graphics类的DrawRectangle方法,可重载。 (1)绘制由Rectangle结构指定的矩形。
public void DrawRectangle(Pen pen,Rectangle rect)
pen:Pen对象,确定线条颜色、宽度和样式。rect:表示要绘制矩形的Rectangle结构。 例如:
Rectangle rect = new Rectangle(0, 0, 80, 50);
(2)绘制由坐标对、宽度和高度指定的矩形。
public void DrawRectangle(Pen pen, int x, int y, int width, int height)
pen:Pen对象,确定线条颜色、宽度和样式。x:要绘制矩形的左上角x坐标。y:要绘制矩形的左上角y坐标。width和height分别表示宽度和高度。 绘制矩形的示例代码:
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); }
绘制椭圆
Graphics类中的DrawEllipse方法,可重载。主要用来绘制边界由Rectangle结构指定的椭圆。 (1)绘制边界由Rectangle结构指定的椭圆。
public void DrawEllipse(Pen pen, Rectangle rect)
(2)绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆。
public void DrawEllipse(Pen pen, int x, int y, int width, int height)
绘制椭圆的示例代码:
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); }
绘制圆弧
Graphics类中的DrawArc方法,可重载。 (1)绘制一段弧线,它表示由Rectangle结构指定的椭圆的一部分。
public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle)
pen:Pen对象,确定线条颜色、宽度和样式。rect:Rectangle结构,定义椭圆边界。startAngle:从x轴到弧线的起始点沿顺时针方向度量的角(以度为单位)。sweepAngle:从startAngle参数到弧线的结束点沿顺时针方向度量的角(以度为单位)。 (2)绘制一段弧线,它表示由一对坐标、宽度和高度指定的椭圆部分。
public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)
绘制圆弧的实例代码:
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); }
绘制多边形
需要Graphics对象、Pen对象和Point(或PointF)对象数组。Graphics类提供DrawPolygon方法,Pen对象存储用于呈现多边形的线条属性,如宽度和颜色等,Point(或PointF)对象数组存储多边形的各个顶点。可重载。 (1)绘制由一组Point结构定义的多边形。
public void DrawPolygon(Pen pen, Point[] pints)
(2)绘制由一组PointF结构定义的多边形。
public void DrawPolygon(Pen pen, PointF[] pints)
绘制多边形示例代码:
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); }
绘制基数样条
基数样条是一连串单独的曲线,连接起来组成较大的曲线。由点的数组和张力参数指定,样条平滑地经过数组的每个点,曲线的陡度上没有尖角和突然的变化。 (1)绘制经过一组指定Point结构的基数样条。
public void DrawCurve(Pen pen, Point[] points)
(2)使用指定的张力,绘制经过一组指定Point结构的基数样条。
public void DrawCurve(Pen pen, Point[] points, float tension)
tension:大于或等于0.0F的值,指定曲线的张力。 (3)从相对于数组开始位置的偏移量开始,绘制经过一组指定PointF结构的基数样条。
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中文网!

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

Video Face Swap
Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Les méthodes d'utilisation des symboles dans la couverture du langage C Couverture arithmétique, l'affectation, les conditions, la logique, les opérateurs de bits, etc. Les opérateurs arithmétiques sont utilisés pour les opérations mathématiques de base, les opérateurs d'affectation sont utilisés pour les opérations et les opérations de la soustraction, la multiplication et les opérations de division, les opérations BIT sont utilisé pointeurs nuls, marqueurs de fin de fichier et valeurs non nucères.

En C, le type de char est utilisé dans les chaînes: 1. Stockez un seul caractère; 2. Utilisez un tableau pour représenter une chaîne et se terminer avec un terminateur nul; 3. Faire fonctionner via une fonction de fonctionnement de chaîne; 4. Lisez ou sortant une chaîne du clavier.

La différence entre le multithreading et l'asynchrone est que le multithreading exécute plusieurs threads en même temps, tandis que les opérations effectuent de manière asynchrone sans bloquer le thread actuel. Le multithreading est utilisé pour les tâches à forte intensité de calcul, tandis que de manière asynchrone est utilisée pour l'interaction utilisateur. L'avantage du multi-threading est d'améliorer les performances informatiques, tandis que l'avantage des asynchrones est de ne pas bloquer les threads d'interface utilisateur. Le choix du multithreading ou asynchrone dépend de la nature de la tâche: les tâches à forte intensité de calcul utilisent le multithreading, les tâches qui interagissent avec les ressources externes et doivent maintenir la réactivité de l'interface utilisateur à utiliser asynchrone.

Dans le langage C, la principale différence entre Char et WCHAR_T est le codage des caractères: Char utilise ASCII ou étend ASCII, WCHAR_T utilise Unicode; Char prend 1 à 2 octets, WCHAR_T occupe 2-4 octets; Char convient au texte anglais, WCHAR_T convient au texte multilingue; Le char est largement pris en charge, WCHAR_T dépend de la prise en charge du compilateur et du système d'exploitation Unicode; Le char est limité dans la gamme de caractères, WCHAR_T a une gamme de caractères plus grande et des fonctions spéciales sont utilisées pour les opérations arithmétiques.

Dans le langage C, les caractères spéciaux sont traités à travers des séquences d'échappement, telles que: \ n représente les pauses de ligne. \ t signifie le caractère d'onglet. Utilisez des séquences d'échappement ou des constantes de caractères pour représenter des caractères spéciaux, tels que char c = '\ n'. Notez que l'arrière-plan doit être échappé deux fois. Différentes plates-formes et compilateurs peuvent avoir différentes séquences d'échappement, veuillez consulter la documentation.

Dans le langage C, la conversion de type char peut être directement convertie en un autre type par: Casting: Utilisation de caractères de casting. Conversion de type automatique: Lorsqu'un type de données peut accueillir un autre type de valeur, le compilateur le convertit automatiquement.

Il n'y a pas de fonction de somme intégrée dans le langage C, il doit donc être écrit par vous-même. La somme peut être obtenue en traversant le tableau et en accumulant des éléments: Version de boucle: la somme est calculée à l'aide de la longueur de boucle et du tableau. Version du pointeur: Utilisez des pointeurs pour pointer des éléments de tableau, et un résumé efficace est réalisé grâce à des pointeurs d'auto-incitation. Allouer dynamiquement la version du tableau: allouer dynamiquement les tableaux et gérer la mémoire vous-même, en veillant à ce que la mémoire allouée soit libérée pour empêcher les fuites de mémoire.

Le Array Char stocke des séquences de caractères en C et est déclaré Char Array_name [Taille]. L'élément d'accès est passé par l'opérateur d'indice, et l'élément se termine par le terminateur nul «\ 0», qui représente le point final de la chaîne. Le langage C fournit une variété de fonctions de manipulation de cordes, telles que strlen (), strcpy (), strcat () et strcmp ().
