【学习笔记】Windows GDI绘图(六)图形路径GraphicsPath详解(中)

上一篇【学习笔记】Windows GDI绘图(五)图形路径GraphicsPath详解(上)介绍了GraphicsPath类的构造函数、属性和方法AddArc添加椭圆弧、AddBezier添加贝赛尔曲线、AddClosedCurve添加封闭基数样条曲线、AddCurve添加开放基数样条曲线、基数样条如何转Bezier、AddEllipse添加椭圆、AddLine添加线段。

革命尚未成功,同志仍需努力!

文章目录

  • GraphicsPath方法
    • AddLines添加线段
    • AddPath附加路径
    • AddPie添加饼形
    • AddPolygon添加多边形
    • AddRectangle和AddRectangles 添加矩形
    • AddString添加字符串
    • SetMarkers设置标记
    • ClearMarkers清空标记
    • StartFigure开始新的图形
    • CloseAllFigures闭合所有图形、CloseFigure闭合当前图形

GraphicsPath

GraphicsPath方法

AddLines添加线段

原型:

public void AddLines (params System.Drawing.Point[] points);
public void AddLines (params System.Drawing.PointF[] points);

添加一系列的线段到GraphicsPath中。

// 定义三角形的顶点
Point[] points ={new Point(150,150),new Point(300,300),new Point(0,300),new Point(150,150)};using (var myPath = new GraphicsPath())
{myPath.AddLines(points);e.Graphics.DrawPath(penRed, myPath);
}

通过点集绘制线段
AddLines

AddPath附加路径

原型:

public void AddPath (System.Drawing.Drawing2D.GraphicsPath addingPath, bool connect);

connect,当前路径与附加的路径是否相连
将指定的GraphicsPath附加到当前Path中

Point[] myArray ={new Point(120,120),new Point(240,240),new Point(0,240),new Point(120,120)};
GraphicsPath myPath = new GraphicsPath();
myPath.AddLines(myArray);Point[] myArray2 ={new Point(120,100),new Point(20,20),new Point(220,20),new Point(120,100)};
GraphicsPath myPath2 = new GraphicsPath();
myPath2.AddLines(myArray2);// Add the second path to the first path.
myPath.AddPath(myPath2, false);//各自独立// Draw the combined path to the screen.
e.Graphics.DrawPath(penRed, myPath);myPath.Reset();
myPath.AddLines(myArray);
myPath.AddPath(myPath2, true);//相连myPath.Transform(new Matrix(1, 0, 0, 1, 400, 0));
e.Graphics.DrawPath(penLightGreen, myPath);

AddPath

AddPie添加饼形

原型:

public void AddPie (System.Drawing.Rectangle rect, float startAngle, float sweepAngle);
public void AddPie (int x, int y, int width, int height, float startAngle, float sweepAngle);
public void AddPie (float x, float y, float width, float height, float startAngle, float sweepAngle);

通过一个矩形、起始角度和扫描角度来角度一个饼形,与椭圆参数类似。

var rect = new Rectangle(100, 100, 200, 100);
using (var path = new GraphicsPath())
{//添加饼形 30°至150°path.AddPie(rect, 30, 120);e.Graphics.DrawPath(penRed, path);path.Reset();//150°至270°path.AddPie(rect, 30 + 120, 120);e.Graphics.DrawPath(penLightGreen, path);path.Reset();//30°到 270°(逆时针)path.AddPie(rect, 30, -120);e.Graphics.DrawPath(Pens.Chocolate, path);
}           

sweepAngle,正数,顺时针;负数,逆时针
AddPie

AddPolygon添加多边形

原型:

public void AddPolygon (System.Drawing.Point[] points);
public void AddPolygon (System.Drawing.PointF[] points);

定义点集,形成多边形

// 多边形顶点
Point[] myArray ={new Point(230, 200),new Point(400, 100),new Point(570, 200),new Point(500, 400),new Point(300, 400)};using (var myPath = new GraphicsPath())
{//添加多边形myPath.AddPolygon(myArray);e.Graphics.DrawPath(penRed, myPath);
}

AddPolygon

AddRectangle和AddRectangles 添加矩形

原型:

public void AddRectangle (System.Drawing.RectangleF rect);
public void AddRectangle (System.Drawing.Rectangle rect);
public void AddRectangles (System.Drawing.Rectangle[] rects);
public void AddRectangles (params System.Drawing.RectangleF[] rects);

定义矩形和矩形集,添加到路径中。

var rect = new Rectangle(30, 50, 100, 80);var rects = new RectangleF[]
{new RectangleF(150,50,80,60),new RectangleF(200,80,100,80)
};using (var myPath = new GraphicsPath())
{                myPath.AddRectangle(rect);myPath.AddRectangles(rects);e.Graphics.DrawPath(penRed, myPath);
}

AddRectangle

AddString添加字符串

原型:

public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.Point origin, System.Drawing.StringFormat? format);
public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.PointF origin, System.Drawing.StringFormat? format);
public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.Rectangle layoutRect, System.Drawing.StringFormat? format);
public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat? format);
参数说明
s待添加的文本
familyFontFamily字体名称
style文本样式,Bold-1,Italic-2,Regular-0,Strikeout-8,Underline-4
emSize文本高度,单位:像素
origin文本起始点,默认是左对齐时,是左上角
format指定文本格式信息,例如行距和对齐方式

这里先随便给个示例吧,估计关于绘制文本,可以另起一篇。

// Create a GraphicsPath object.
GraphicsPath myPath = new GraphicsPath();// Set up all the string parameters.
string stringText = "我在学习GDI+";
FontFamily family = new FontFamily("Arial");
int fontStyle = (int)FontStyle.Italic;
int emSize = 38;//文本高度,像素
Point origin = new Point(200, 100);//文本开始绘制的左上角点StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
format.Alignment = StringAlignment.Center;//水平居中
format.LineAlignment = StringAlignment.Center; // 垂直居中// Add the string to the path.
myPath.AddString(stringText,family,fontStyle,emSize,origin,format);e.Graphics.FillPath(Brushes.Green, myPath);//文本定位点
e.Graphics.FillEllipse(Brushes.Red, origin.X - 3, origin.Y - 3, 6, 6);

AddString

SetMarkers设置标记

原型:

public void SetMarkers ();

使用 SetMarkers 方法在 GraphicsPath 中的当前位置创建标记。使用 NextMarker 方法迭代路径中的现有标记。
标记用于分隔子路径组。两个标记之间可以包含一个或多个子路径。

[System.ComponentModel.Description("GraphicsPath的SetMarkers/ClearMarkers方法")]
public void Demo06_07(PaintEventArgs e)
{// Create a path and set two markers.GraphicsPath myPath = new GraphicsPath();myPath.AddLine(new Point(0, 0), new Point(50, 50));myPath.SetMarkers();Rectangle rect = new Rectangle(50, 50, 50, 50);myPath.AddRectangle(rect);myPath.SetMarkers();myPath.AddEllipse(100, 100, 100, 50);// Draw the path to screen.e.Graphics.DrawPath(new Pen(Color.Black, 2), myPath);var pathIterator =new GraphicsPathIterator(myPath);pathIterator.Rewind();var potins = myPath.PathPoints;var types = myPath.PathTypes;var height = 20;var markerIndex = 0;int startIndex;int endIndex;while(true){var resultCount = pathIterator.NextMarker(out startIndex, out endIndex);if (resultCount == 0) break;//Marker信息e.Graphics.DrawString($"Marker {markerIndex}:  Start: {startIndex} End: {endIndex}",Font,Brushes.Red,200,height);height += 20;//每段Marker的点与类型信息for (int i = startIndex; i <= endIndex; i++){e.Graphics.DrawString($"point {i}: ({potins[i].X},{potins[i].Y}) Type:{(int)types[i]}:{GetPathTypes((int)types[i])}",Font,Brushes.Black,250,height);height += 20;}markerIndex++;}myPath.ClearMarkers();pathIterator = new GraphicsPathIterator(myPath);pathIterator.Rewind();var count= pathIterator.NextMarker(out startIndex, out endIndex);//这里合成一个,0至19
}
/// <summary>
/// PathType转字符串
/// </summary>
/// <param name="pathType"></param>
/// <returns></returns>
private string GetPathTypes(int pathType)
{if (pathType == 0) return "0(起点)";List<string> typeStrs = new List<string>();while(true){if(pathType >= 0x80){typeStrs.Add("128(终点)");pathType -= 0x80;}else if (pathType >= 0x20){typeStrs.Add("32(标记)");pathType -= 0x20;}else if (pathType >=0x7){typeStrs.Add("7(屏蔽)");pathType -= 0x7;}else if(pathType >= 0x3){typeStrs.Add("3(Bezier)");pathType -= 0x3;}else if (pathType >= 1){typeStrs.Add("1(Line)");pathType -= 0x1;}if (pathType <= 0) break;}return string.Join("+",typeStrs.ToArray());
}

ClearMarkers清空标记

原型:

public void ClearMarkers ();

清除所有标记(Marker),合并为一个。

StartFigure开始新的图形

原型:

public void StartFigure ();

在不封闭当前图形(路径)下,新开一个图形,后续增加的路径将在此图形中。

CloseAllFigures闭合所有图形、CloseFigure闭合当前图形

原型:

public void CloseAllFigures ();
public void CloseFigure ();

CloseAllFigures :闭合该路径中所有开放的图形并开始一个新图形。它通过从端点到起点连接一条线来闭合每个开放图形。
CloseFigure:闭合当前图形并开始新图形。

// 创建含多个开放路径的图形
GraphicsPath myPath = new GraphicsPath();
myPath.StartFigure();
myPath.AddLine(new Point(10, 10), new Point(150, 10));
myPath.AddLine(new Point(150, 10), new Point(10, 150));
myPath.StartFigure();
myPath.AddArc(200, 200, 100, 100, 0, 90);
myPath.StartFigure();
Point point1 = new Point(300, 300);
Point point2 = new Point(400, 325);
Point point3 = new Point(400, 375);
Point point4 = new Point(300, 400);
Point[] points = { point1, point2, point3, point4 };
myPath.AddCurve(points);// 绘制非封闭路径
e.Graphics.DrawPath(new Pen(Color.Green, 10), myPath);// 封闭所有路径.
myPath.CloseAllFigures();// 绘制封闭后路径
e.Graphics.DrawPath(new Pen(Color.Red, 1), myPath);myPath = new GraphicsPath();
myPath.StartFigure();
myPath.AddLine(new Point(200, 10), new Point(400, 10));
myPath.AddLine(new Point(400, 10), new Point(400, 200));
myPath.CloseFigure();e.Graphics.DrawPath(penRed, myPath);

CloseAllFigure

【学习笔记】Windows GDI绘图目录

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/14579.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

GNSS仿真测试之三种常用坐标系与转换

作者介绍 在当今的全球导航卫星系统&#xff08;GNSS&#xff09;技术领域&#xff0c;仿真测试是评估和验证GNSS接收机性能的关键环节&#xff0c;全球导航卫星系统&#xff08;GNSS&#xff09;仿真测试是确保GNSS接收机和导航解决方案在实际部署前能够正确、可靠地工作的关键…

Gradle常见问题及总结

使用android studio开发项目&#xff0c;难免遇到gradle相关的错误&#xff0c;在此总结。 gradle插件与gradle home版本关系错误 参考更新 Gradle Gradle下载太慢 Index of /gradle/ (tencent.com) 是国内下载地址,手动下载对应版本即可 缓存不刷新 问题描述 maven发布…

jenkins插件之xunit

分析测试工具执行的结果&#xff0c;并图形化&#xff0c;比如phpunit&#xff0c;phpstan,可分析junit格式的结果 安装jenkins插件 搜索xunit并安装 项目配置 配置 - Build Steps 您的项目 - 配置 - Build Steps, 新增 Run with timeout 超时时间根据实际情况配置 Build…

Day38 贪心算法part05

LC435无重叠区间(未掌握) 思路&#xff1a;先对数组进行排序&#xff0c;找到非重叠的区间的个数&#xff0c;然后区间的总数减去非重叠区间的个数即是需要移除的区间的个数与LC452用最少数量的箭引爆气球类似&#xff0c;但是不同的是[1,2]和[2,3]在此题并不是重叠区间但是在…

oracle怎么处理json格式

向数据库导入json相关jar包 loadjava -r -f -u bsuser/XXXX192.168.10.31/bsorcl json.jar 要删除的话&#xff0c;删除指定jar dropjava -u bsuser/XXXX192.168.10.31/bsorcl json.jar select * from user_java_classes 然后我们就可以取到json串中任意节点的值

SpringBoot项目中redis序列化和反序列化LocalDateTime失败

实体类中包含了LocalDateTime 类型的属性&#xff0c;把实体类数据存入Redis后变成这样&#xff1a; 此时&#xff0c;存入redis不会报错&#xff0c;但是从redis获取的时候&#xff0c;会报错&#xff1a; com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Ca…

Springboot项目打包:将依赖的jar包输出到指定目录

场景 公司要对springboot项目依赖的jar包进行升级&#xff0c;但是遇到一个问题&#xff0c;项目打包之后&#xff0c;没办法看到他里面依赖的jar包&#xff0c;版本到底是不是升上去了&#xff0c;没办法看到。 下面是项目打的jar包 我们通过反编译工具jdgui&#xff0c;来…

VUE3和VUE2

VUE3和VUE2 上一篇文章中&#xff0c;我们对VUE3进行了一个初步的认识了解&#xff0c;本篇文章我们来进一步学习一下&#xff0c;顺便看一下VUE2的写法VUE3是否能做到兼容&#x1f600;。 一、新建组件 我们在components中新建一个组件&#xff0c;名称为Peron&#xff0c;…

缓存降级

当Redis缓存出现问题或者无法正常工作时,需要有一种应对措施,避免直接访问数据库而导致整个系统瘫痪。缓存降级就是这样一种机制。 主要的缓存降级策略包括: 本地缓存降级 当Redis缓存不可用时,可以先尝试使用本地进程内缓存,如Guava Cache或Caffeine等。这样可以减少对Redis…

阴影映射(线段树)

实时阴影是电子游戏中最为重要的画面效果之一。在计算机图形学中&#xff0c;通常使用阴影映射方法来实现实时阴影。 游戏开发部正在开发一款 2D 游戏&#xff0c;同时希望能够在 2D 游戏中模仿 3D 游戏的光影效果&#xff0c;请帮帮游戏开发部&#xff01; 给定 x-y 平面上的…

再次学习History.scrollRestoration

再次学习History.scrollRestoration 之前在react.dev的源代码中了解到了这个HIstory的属性&#xff0c;当时写了一篇笔记来记录我对它的理解&#xff0c;现在看来还是一知半解。所以今天打算重新学习一下这个属性&#xff0c;主要从属性以及所属对象的介绍、使用方法&#xff0…

Linux信号:信号的保存

目录 一、信号在内核中的表示 二、sigset_t 2.1sigset_t的概念和意义 2.2信号集操作数 三、信号集操作数的使用 3.1sigprocmask 3.2sigpending 3.3sigemptyset 四、代码演示 一、信号在内核中的表示 实际执行信号的处理动作称为信号 递达(Delivery) 。 信号从产生到递达…

Mysql数据库——DML操作

目录 添加数据&#xff08;INSERT&#xff09; 修改数据&#xff08;UPDATE&#xff09; 删除数据&#xff08;DELETE&#xff09; 添加数据&#xff1a; &#xff08;1). 给指定字段添加数据 &#xff08;2). 给全部字段添加数据 &#xff08;3). 批量添加数据 修改数据: 案例…

【STM32】HAL库点灯

【STM32】HAL库点灯 一、探究目标二、探究原理2.1 ST开发库2.1.1 直接配置寄存器2.1.2 标准外设库2.1.3 HAL库2.2 HAL开发2.2.1 环境配置2.2.2 时钟配置2.2.3 GPIO配置2.2.4 工程创建2.2.5 KEIL代码![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/bf1c95d5c6724a6a…

NextGen Mirth Connect XStream反序列化远程代码执行漏洞(CVE-2023-43208)

0x01 产品简介 NextGen Mirth Connect是是美国NextGen公司的一个医疗集成引擎,主要用于医疗领域的系统集成和数据交换,支持多种协议和标准。 0x02 漏洞概述 NextGen Mirth Connect 4.4.1之前版本存在远程代码执行漏洞,未经身份认证的攻击者可利用该漏洞远程执行代码。 0…

混合组网VS传统网络:智能硬件混合组网优劣势浅要解析

智能硬件混合组网是一种利用多种通信技术相结合的方法&#xff0c;以实现更灵活、更可靠的网络连接。通过蓝牙、Wi-Fi、LoRa、4G相互之间的不同通讯方式&#xff0c;根据应用场景的不同以及现场实际环境&#xff0c;优选最佳物联网混合组网方案&#xff0c;以达到部署最便捷性价…

一张SSL证书如何同时保护多个域名及其子域名?

在互联网时代&#xff0c;数据安全和隐私保护变得至关重要&#xff0c;而SSL证书作为确保网站安全的重要工具&#xff0c;其重要性不言而喻。本文将详细探讨一种特殊的SSL证书——多域名通配符SSL证书&#xff0c;它为网站管理员提供了一种高效、经济的方式来保护多个域名及其子…

MyBatis从入门到“入土“

&#x1f495;喜欢的朋友可以关注一下&#xff0c;下次更新不迷路&#xff01;&#x1f495;(●◡●) 目录 一、Mybatis为何物&#xff1f;&#x1f44c; 二、快速入门&#x1f923; 1、新建项目&#x1f60a; 2、数据库建表&#x1f60a; 3、导入依赖的jar包&#x1f60a;…

Linux学习笔记6

TFTP 服务器搭建和测试 关于TFTP:TFTP&#xff08;Trivial File Transfer Protocol&#xff0c;简单文件传输协议&#xff09;&#xff0c;是一个基于UDP 协议实现 的用于在客户机和服务器之间进行简单文件传输的协议&#xff0c;适合于开销不大、不复杂的应用场合 搭建服务器…

后量子密码的发展和应用

后量子算法&#xff0c;特别是后量子密码(PQC)&#xff0c;是近年来密码学领域的一个热门话题。随着量子计算技术的快速发展&#xff0c;传统的公钥密码算法面临着被量子计算机破解的威胁。为了应对这一挑战&#xff0c;后量子密码应运而生&#xff0c;成为了一种能够抵抗量子计…