【学习笔记】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,一经查实,立即删除!

相关文章

华为校招机试 - 最久最少使用缓存(20240508)

题目描述 无线通信移动性需要在基站上配置邻区(本端基站的小区 LocalCell 与周边邻基站的小区 NeighborCelI 映射)关系, 为了能够加速无线算法的计算效率,设计一个邻区关系缓存表,用于快速的通过本小区 LocalCell 查询到邻小区 NeighborCell。 但是缓存表有一定的规格限…

代码随想录-Day07

454. 四数相加 II 给你四个整数数组 nums1、nums2、nums3 和 nums4 &#xff0c;数组长度都是 n &#xff0c;请你计算有多少个元组 (i, j, k, l) 能满足&#xff1a; 0 < i, j, k, l < n nums1[i] nums2[j] nums3[k] nums4[l] 0 示例 1&#xff1a; 输入&#x…

系统磁盘高级管理、lvm例子、创建pv、创建VG、创建lv、磁盘扩展

LVM&#xff1a; 逻辑卷&#xff0c;动态调整分区大小&#xff0c;扩展性好 创建pv pvcreate &#xff1a; 将实体 partition 创建成为 PV &#xff1b; pvscan &#xff1a; 搜寻目前系统里面任何具有 PV 的磁盘&#xff1b; pvdisplay &#xff1a; 显示出目前系统上面…

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

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

【git】学习记录: 贮藏功能

Git 贮藏修改是一种临时存储工作目录中已经修改但尚未提交的更改的机制。通过贮藏修改&#xff0c;你可以将当前的工作目录状态保存起来&#xff0c;以便你可以在之后的时间点重新应用这些更改&#xff0c;或者在不同的分支间切换时避免冲突。 要使用 Git 贮藏修改&#xff0c…

Linux(centos)常用命令

Linux&#xff08;Centos&#xff09;常用命令使用说明文档 切换到/home目录下 使用cd命令切换目录&#xff0c;例如&#xff1a; cd /home列出/home目录下的所有文件 使用ls命令列出目录下的文件和子目录&#xff0c;例如&#xff1a; ls /home新建目录dir1 使用mkdir命…

头歌OpenGauss数据库-I.复杂查询第1关:获取前N名成绩

本关任务&#xff1a;编写函数来实现获取前N名成绩的方法。 提示&#xff1a;前面的实验没有提供编写自定义函数的示例&#xff0c;需要参考OpenGauss数据库文档学习自定义函数的使用。 score表内容如下&#xff1a; IdScore13.5223.6534.2343.8554.2363.65 --#请在BEGIN - END…

python windows 开发.exe程序笔记

import win32api import win32gui import win32con import time import tkinter as tk## pyinstaller --onefile t4.py 将python 代码打包为windows可执行文件 .exe ## airtext 大漠 def clickGoogle():hw win32gui.FindWindow("Chrome_WidgetWin_1", "新标…

解决Redis 缓存雪崩(过期时间不一致) 和 缓存穿透(黑名单)

解决Redis 缓存雪崩&#xff08;过期时间不一致&#xff09; 和 缓存穿透&#xff08;黑名单&#xff09; public Product getdetailById(Integer id) {String key "product." id;// 查询黑名单中是否有该keyBoolean b hashOperations.hasKey(PROODUCT_DETAIL_B…

算法 Hw7

Hw 7 Graph Algorithm 1 Edge detection2 Reachability3 Bitonic shortest paths 1 Edge detection 由 Cut Property 可知&#xff1a;如果 e 是从某个集合 S 到补集 V−S 的开销最小的边&#xff0c;则 e 一定所有最小生成树中。 由 Cycle Property 可知&#xff1a;如果 e 是…

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串中任意节点的值

Linux完整版命令大全(四)

2. linux系统设置命令 alias 功能说明&#xff1a;设置指令的别名。语  法&#xff1a;alias[别名][指令名称]补充说明&#xff1a;用户可利用alias&#xff0c;自定指令的别名。若仅输入alias&#xff0c;则可列出目前所有的别名设置。 alias的效力仅及于该次登入的操作。…

行列视(RCV)部署在互联网还是部署在企业内部?

行列视&#xff08;RCV&#xff09;的部署方式可以根据企业的具体需求和情况来灵活选择。它既可以部署在互联网上&#xff0c;也可以部署在企业内部。 对于希望实现远程访问、多地点协同工作或者与第三方服务集成等需求的企业&#xff0c;可以选择将行列视&#xff08;RCV&…

Postgresql源码(129)JIT函数中如何使用PG的类型llvmjit_types

0 总结 llvmjit_types文件分三部分 类型定义&#xff1a;llvm通过变量找到对应结构体的定义&#xff0c;在通过结构体内的偏移量宏使用成员变量。模版函数定义&#xff1a; 第一&#xff1a;AttributeTemplate被当做一个函数属性的模板&#xff08;例如nofree、nosync等clang…

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