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

2024-05-27 02:04

本文主要是介绍【学习笔记】Windows GDI绘图(七)图形路径GraphicsPath详解(下),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前三篇回顾
  • GraphicsPath方法
    • Flatten压平(将曲线转成线段)
    • GetBounds获取外接矩形
    • GetLastPoint获取路径最后一个点
    • IsOutlineVisible
    • IsVisiable是否在轮廓上或内部
    • Reset重置
    • Reverse逆转点的顺序
    • Transform矩阵变换
    • Wrap扭曲变换
    • Widen将路径替换为指定画笔的填充区域

前三篇回顾

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

  • 图形路径GraphicsPath
    填充模式FillMode
  • 构造函数
    GraphicsPath()
    GraphicsPath(FillMode)
    GraphicsPath(Point[],Byte[])和GraphicsPath(PointF[], Byte[])
    GraphicsPath(Point[], Byte[], FillMode)和GraphicsPath(PointF[], Byte[], FillMode)
    PathPointType
  • 属性
    FillMode
    PathData
    PathPoints、PathTypes
    PointCount
  • 方法
    AddArc添加椭圆弧
    AddBezier添加贝赛尔曲线
    AddClosedCurve添加封闭基数样条曲线
    AddCurve添加基数样条曲线(开放)
    AddEllipse添加椭圆
    AddLine添加线段

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

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

Window GDI+ API有BUG?GetBounds测不准?

  • 详细说明GetBounds方法
    全文图像
    全文图像

GraphicsPath方法

Flatten压平(将曲线转成线段)

原型:

public void Flatten ();
public void Flatten (System.Drawing.Drawing2D.Matrix? matrix);//默认flatness=0.25
public void Flatten (System.Drawing.Drawing2D.Matrix? matrix, float flatness);
参数说明
martix变换矩阵
flatness曲线转线段的最大误差,默认值是0.25。
值越小,越接近曲线,线段数量越多。

将此路径中的每条曲线转换为一系列连接的线段。

GraphicsPath myPath = new GraphicsPath();
Matrix translateMatrix = new Matrix();
translateMatrix.Translate(0, 0);
Point point1 = new Point(20, 200);
Point point2 = new Point(100, 30);
Point point3 = new Point(230, 300);
Point point4 = new Point(380, 150);
Point[] points = { point1, point2, point3, point4 };
myPath.AddCurve(points);e.Graphics.DrawPath(new Pen(Color.LightGreen, 5), myPath);
myPath.Flatten(translateMatrix, 25f);
e.Graphics.DrawPath(new Pen(Color.Black, 1), myPath);//原曲线控制点
foreach (var pt in points)
{e.Graphics.FillEllipse(Brushes.Red, pt.X - 5, pt.Y - 5, 10, 10);
}//Flatten后的点
foreach( var pt in myPath.PathPoints )
{e.Graphics.FillEllipse(Brushes.Black, pt.X - 3, pt.Y - 3, 6, 6);
}

Flatten

GetBounds获取外接矩形

原型:

public System.Drawing.RectangleF GetBounds ();
public System.Drawing.RectangleF GetBounds (System.Drawing.Drawing2D.Matrix? matrix);
public System.Drawing.RectangleF GetBounds (System.Drawing.Drawing2D.Matrix? matrix, System.Drawing.Pen? pen);

获取路径的外接矩形,关于使用pen参数后,获取结果貌似“松弛”问题,可查阅Window GDI+ API有BUG?GetBounds测不准?

var rect = new Rectangle(200, 200, 300, 200);using(var path=new GraphicsPath())
{path.AddEllipse(rect);//用于确定椭圆的矩形e.Graphics.DrawRectangle(new Pen(Color.LightGreen,10),rect);var pathPen = new Pen(Color.Green, 50);pathPen.MiterLimit = 1;var bboxWithPen = path.GetBounds(new Matrix(), pathPen);//含pen参数的外接矩形e.Graphics.DrawRectangle(Pens.Black,bboxWithPen.X + pathPen.Width / 2, bboxWithPen.Y + pathPen.Width / 2, bboxWithPen.Width - pathPen.Width, bboxWithPen.Height - pathPen.Width);//绘制椭圆e.Graphics.DrawPath(pathPen, path);var bboxWitoutPen = path.GetBounds();//不含pen参数的外接矩形e.Graphics.DrawRectangles(new Pen(Color.Red, 3), new RectangleF[] { bboxWitoutPen });
}

定义一个矩形,根据这个矩形绘制一个椭圆,其中pen的宽度为50,获取含pen与不含pen时其外接矩形的大小。
GetBounds

GetLastPoint获取路径最后一个点

原型:

public System.Drawing.PointF GetLastPoint ();

作用:获取GraphicsPath中路径的最后一个点。

var pt1 = new Point(200, 200);
var pt2 = new Point(300, 300);using (var path = new GraphicsPath())
{path.AddLine(pt1,pt2);//绘制路径e.Graphics.DrawPath(new Pen(Color.Red, 10), path);//获取路径最后一个点的坐var lastPoint=path.GetLastPoint();int offset = 20;//显示最后一点的坐标DrawString(e, $"LastPoint:({lastPoint.X},{lastPoint.Y})", ref offset);
}

定义两个点,按线段方式添加到图形路径中,再通过GetLastPoint()获取其最后一个点的坐标。

在这里插入图片描述

IsOutlineVisible

原型:

public bool IsOutlineVisible (int x, int y, System.Drawing.Pen pen, System.Drawing.Graphics? graphics);
public bool IsOutlineVisible (System.Drawing.Point pt, System.Drawing.Pen pen, System.Drawing.Graphics? graphics);
public bool IsOutlineVisible (float x, float y, System.Drawing.Pen pen, System.Drawing.Graphics? graphics);
public bool IsOutlineVisible (float x, float y, System.Drawing.Pen pen);
public bool IsOutlineVisible (System.Drawing.PointF point, System.Drawing.Pen pen);
public bool IsOutlineVisible (System.Drawing.PointF pt, System.Drawing.Pen pen, System.Drawing.Graphics? graphics);
public bool IsOutlineVisible (int x, int y, System.Drawing.Pen pen);
public bool IsOutlineVisible (System.Drawing.Point point, System.Drawing.Pen pen);

作用:使用指定的Pen绘制时,指定的点是否包含在此GraphicsPath的轮廓内。
要判断的点在边缘处时,不同的笔宽返回值略有不同,使用时需注意。

[System.ComponentModel.Description("GraphicsPath的IsOutlineVisiable方法")]
public void Demo07_04(PaintEventArgs e)
{var rect = new Rectangle(300, 200, 300, 200);using (var path = new GraphicsPath(FillMode.Winding)){path.AddRectangle(rect);var penWidth = 1f;var halfPenWidth = penWidth / 2f;var pathPen = new Pen(Color.Red, penWidth);//绘制路径e.Graphics.DrawPath(pathPen, path);int offset = 10;DrawString(e,$"Rect:({rect.X},{rect.Y},{rect.Width},{rect.Height}) penWidth:{pathPen.Width}",ref offset);var y = (rect.Top + rect.Bottom) / 2.0f;var pt = new PointF(rect.X - halfPenWidth - 1f, y);DrawString(e, $"({pt.X},{pt.Y}) IsOutlineVisiable:{path.IsOutlineVisible(pt, pathPen)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X - halfPenWidth-0.5f, y);DrawString(e, $"({pt.X},{pt.Y}) IsOutlineVisiable:{path.IsOutlineVisible(pt, pathPen)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X + halfPenWidth - 1f, y);DrawString(e, $"({pt.X},{pt.Y}) IsOutlineVisiable:{path.IsOutlineVisible(pt, pathPen)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X + halfPenWidth - 0.5f, y);DrawString(e, $"({pt.X},{pt.Y}) IsOutlineVisiable:{path.IsOutlineVisible(pt, pathPen)})", ref offset);DrawCross(e, pt, Color.Black);pt = new Point(450, 300);// 在轮廓包围的里面,也不算在轮廓上DrawString(e, $"({pt.X},{pt.Y}) IsOutlineVisiable:{path.IsOutlineVisible(pt, pathPen)})", ref offset);DrawCross(e, pt, Color.Black);}
}/// <summary>
/// 给定中心点,画十字架
/// </summary>
/// <param name="e"></param>
/// <param name="center"></param>
/// <param name="color"></param>
/// <param name="len"></param>
/// <param name="penWidth"></param>
private void DrawCross(PaintEventArgs e,PointF center,Color color, int len=40, float penWidth=1f)
{var half = len / 2.0f;using (var pen = new Pen(color, penWidth)){e.Graphics.DrawLine(pen, center.X, center.Y - half, center.X, center.Y + half);e.Graphics.DrawLine(pen, center.X-half, center.Y, center.X+half, center.Y );}
}

笔宽为1时
在这里插入图片描述
笔宽为10时
在这里插入图片描述

IsVisiable是否在轮廓上或内部

原型:

public bool IsVisible (System.Drawing.Point point);
public bool IsVisible (System.Drawing.PointF point);
public bool IsVisible (System.Drawing.Point pt, System.Drawing.Graphics? graphics);
public bool IsVisible (System.Drawing.PointF pt, System.Drawing.Graphics? graphics);
public bool IsVisible (int x, int y);
public bool IsVisible (float x, float y);
public bool IsVisible (int x, int y, System.Drawing.Graphics? graphics);
public bool IsVisible (float x, float y, System.Drawing.Graphics? graphics);

作用:判断一个点是否在轮廓上或内部(与IsOutlineVisiable的差别时,IsOutlineVisable只判断是在轮廓上),如果路径是未封闭图形,会自动封闭?

        [System.ComponentModel.Description("GraphicsPath的IsVisiable方法")]public void Demo07_05(PaintEventArgs e){var rect = new Rectangle(300, 200, 300, 200);using (var path = new GraphicsPath(FillMode.Winding)){path.AddRectangle(rect);var penWidth = 10f;var halfPenWidth = penWidth / 2f;var pathPen = new Pen(Color.Red, penWidth);//绘制路径e.Graphics.DrawPath(pathPen, path);int offset = 10;DrawString(e, $"Rect:({rect.X},{rect.Y},{rect.Width},{rect.Height}) penWidth:{pathPen.Width}", ref offset);var y = (rect.Top + rect.Bottom) / 2.0f;var pt = new PointF(rect.X - halfPenWidth - 1f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X - halfPenWidth - 0.5f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X + halfPenWidth - 1f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X + halfPenWidth - 0.5f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new Point(450, 300);// 在轮廓包围的里面,也不算在轮廓上DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);}}[System.ComponentModel.Description("GraphicsPath的IsVisiable方法")]public void Demo07_06(PaintEventArgs e){var rect = new Rectangle(300, 200, 300, 200);var pts = new Point[]{new Point(300,400),new Point(400,150),new Point(500,420)};using (var path = new GraphicsPath(FillMode.Winding)){path.AddCurve(pts);var penWidth = 10f;var halfPenWidth = penWidth / 2f;var pathPen = new Pen(Color.Red, penWidth);//绘制路径e.Graphics.DrawPath(pathPen, path);int offset = 10;DrawString(e, $"Rect:({rect.X},{rect.Y},{rect.Width},{rect.Height}) penWidth:{pathPen.Width}", ref offset);var y = (rect.Top + rect.Bottom) / 2.0f;var pt = new PointF(rect.X - halfPenWidth - 1f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X - halfPenWidth - 0.5f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X + halfPenWidth - 1f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new PointF(rect.X + halfPenWidth - 0.5f, y);DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);pt = new Point(450, 300);// 在轮廓包围的里面,也不算在轮廓上DrawString(e, $"({pt.X},{pt.Y}) IsVisible:{path.IsVisible(pt)})", ref offset);DrawCross(e, pt, Color.Black);}}

创建一个矩形和一个开放的曲线,判断点。

IsVisiable
IsVisable

Reset重置

原型:

public void Reset ();

作用:清空路径并将FillMode置为Alternate。

Reverse逆转点的顺序

原型:

public void Reverse ();

作用:逆转GraphicsPath中PathPoints的点的顺序。

var rect = new Rectangle(200, 200, 400, 250);
var pts = new Point[]
{new Point(300,400),new Point(400,150),new Point(500,420)
};using (var path = new GraphicsPath(FillMode.Winding))
{path.AddRectangle(rect);path.AddCurve(pts);var penWidth = 10f;var pathPen = new Pen(Color.Red, penWidth);//绘制路径e.Graphics.DrawPath(pathPen, path);//逆转path.Reverse();e.Graphics.DrawPath(Pens.Black, path);
}

Reverse

Transform矩阵变换

原型:

public void Transform (System.Drawing.Drawing2D.Matrix matrix);

作用:应用一个矩阵变换到GraphicsPath中

var rect = new Rectangle(200, 200, 300, 200);        using (var path = new GraphicsPath(FillMode.Winding))
{path.AddRectangle(rect);//原始矩形e.Graphics.DrawPath(Pens.Red,path);var matrix = new Matrix();//向右、向下各偏移100matrix.Translate(100, 100, MatrixOrder.Append);path.Transform(matrix);e.Graphics.DrawPath(Pens.Black, path);matrix.Reset();//缩小0.5倍matrix.Scale(0.5f, 0.5f, MatrixOrder.Append);path.Transform(matrix);e.Graphics.DrawPath(Pens.LightGreen, path);
}

定义一个矩形,先向右、向左平移,再缩小0.5倍
Transform

Wrap扭曲变换

原型:

public void Warp (System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect);
public void Warp (System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.Drawing2D.Matrix? matrix);
public void Warp (System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.Drawing2D.Matrix? matrix, System.Drawing.Drawing2D.WarpMode warpMode);
public void Warp (System.Drawing.PointF[] destPoints, System.Drawing.RectangleF srcRect, System.Drawing.Drawing2D.Matrix? matrix, System.Drawing.Drawing2D.WarpMode warpMode, float flatness);

作用:通过定义目标平行四边形的三个顶点(左上角、右上角和左下角)或四边形的四个顶点来扭曲GraphicsPath中的路径。

var srcRect = new Rectangle(200, 200, 300, 200);
Point point1 = new Point(20, 200);
Point point2 = new Point(100, 30);
Point point3 = new Point(230, 300);
Point point4 = new Point(380, 150);
Point[] points = { point1, point2, point3, point4 };using (var path = new GraphicsPath(FillMode.Winding))
{path.AddCurve(points);path.AddRectangle(srcRect);//原始矩形e.Graphics.DrawPath(new Pen(Color.Red,3), path);//定义平行四边形三个顶点(左上角、右上角、左下角)var destPtList = new List<PointF>(){new PointF(300,300),new PointF(600,250),new PointF(200,450)};// Create a translation matrix.Matrix translateMatrix = new Matrix();// Warp the source path (rectangle).path.Warp(destPtList.ToArray(), srcRect, translateMatrix, WarpMode.Perspective, 0.5f);e.Graphics.DrawPath(new Pen(Color.LightGreen, 3), path);//加一个点,自定义四边形destPtList.Add(new PointF(550, 450));//需要重置,不重置的话,是在前面warp变换后,再次变换path.Reset();path.AddCurve(points);path.AddRectangle(srcRect);path.Warp(destPtList.ToArray(), srcRect, translateMatrix, WarpMode.Perspective, 0.5f);e.Graphics.DrawPath(new Pen(Color.LightBlue, 3), path);//path.Reset();path.AddCurve(points);path.AddRectangle(srcRect);translateMatrix.Translate(100, 50);path.Warp(destPtList.ToArray(), srcRect, translateMatrix, WarpMode.Perspective, 0.5f);e.Graphics.DrawPath(new Pen(Color.Pink, 3), path);}

Wrap

Widen将路径替换为指定画笔的填充区域

原型:

public void Widen (System.Drawing.Pen pen, System.Drawing.Drawing2D.Matrix? matrix);
public void Widen (System.Drawing.Pen pen);
public void Widen (System.Drawing.Pen pen, System.Drawing.Drawing2D.Matrix? matrix, float flatness);

作用:将路径替换为指定画笔的填充区域(类似获取膨胀后的轮廓)

 // 创建两个正方形.GraphicsPath myPath = new GraphicsPath();myPath.AddRectangle(new Rectangle(300, 100, 100, 100));myPath.AddRectangle(new Rectangle(450, 100, 100, 100));// Draw the original ellipses to the screen in black.e.Graphics.DrawPath(Pens.Black, myPath);int offset = 5;DrawString(e, $"Src PointCount={myPath.PointCount}", ref offset);for(int i=0;i<myPath.PointCount;i++){var pt = myPath.PathPoints[i];var type = myPath.PathTypes[i];DrawString(e, $"\tPoint:({pt.X},{pt.Y}),types:{type}", ref offset);}// Widen the path.Pen widenPen = new Pen(Color.Black, 20);Matrix widenMatrix = new Matrix();//widenMatrix.Translate(50, 50);myPath.Widen(widenPen, widenMatrix, 1.0f);//绘制Widden后的路径e.Graphics.DrawPath(new Pen(Color.Red,3), myPath);//这里用FillPathe.Graphics.FillPath(new SolidBrush(Color.FromArgb(127,Color.LightGreen)), myPath);DrawString(e, $"Widen PointCount={myPath.PointCount}", ref offset);for (int i = 0; i < myPath.PointCount; i++){var pt = myPath.PathPoints[i];var type = myPath.PathTypes[i];DrawString(e, $"\tPoint:({pt.X},{pt.Y}),types:{type}", ref offset);e.Graphics.FillEllipse(Brushes.Gold, pt.X - 3, pt.Y - 3, 6, 6);}

绘制两个正方形,使用20像素宽的Pen对路径进行Widden后,填充其路径,就标记路径的各个点。

Widden

感谢您的拜读,如果对本系列文章的源码感兴趣,请在评讨区留言Email

这篇关于【学习笔记】Windows GDI绘图(七)图形路径GraphicsPath详解(下)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1006219

相关文章

mac中资源库在哪? macOS资源库文件夹详解

《mac中资源库在哪?macOS资源库文件夹详解》经常使用Mac电脑的用户会发现,找不到Mac电脑的资源库,我们怎么打开资源库并使用呢?下面我们就来看看macOS资源库文件夹详解... 在 MACOS 系统中,「资源库」文件夹是用来存放操作系统和 App 设置的核心位置。虽然平时我们很少直接跟它打交道,但了

关于Maven中pom.xml文件配置详解

《关于Maven中pom.xml文件配置详解》pom.xml是Maven项目的核心配置文件,它描述了项目的结构、依赖关系、构建配置等信息,通过合理配置pom.xml,可以提高项目的可维护性和构建效率... 目录1. POM文件的基本结构1.1 项目基本信息2. 项目属性2.1 引用属性3. 项目依赖4. 构

Rust 数据类型详解

《Rust数据类型详解》本文介绍了Rust编程语言中的标量类型和复合类型,标量类型包括整数、浮点数、布尔和字符,而复合类型则包括元组和数组,标量类型用于表示单个值,具有不同的表示和范围,本文介绍的非... 目录一、标量类型(Scalar Types)1. 整数类型(Integer Types)1.1 整数字

Java操作ElasticSearch的实例详解

《Java操作ElasticSearch的实例详解》Elasticsearch是一个分布式的搜索和分析引擎,广泛用于全文搜索、日志分析等场景,本文将介绍如何在Java应用中使用Elastics... 目录简介环境准备1. 安装 Elasticsearch2. 添加依赖连接 Elasticsearch1. 创

Redis缓存问题与缓存更新机制详解

《Redis缓存问题与缓存更新机制详解》本文主要介绍了缓存问题及其解决方案,包括缓存穿透、缓存击穿、缓存雪崩等问题的成因以及相应的预防和解决方法,同时,还详细探讨了缓存更新机制,包括不同情况下的缓存更... 目录一、缓存问题1.1 缓存穿透1.1.1 问题来源1.1.2 解决方案1.2 缓存击穿1.2.1

PyTorch使用教程之Tensor包详解

《PyTorch使用教程之Tensor包详解》这篇文章介绍了PyTorch中的张量(Tensor)数据结构,包括张量的数据类型、初始化、常用操作、属性等,张量是PyTorch框架中的核心数据结构,支持... 目录1、张量Tensor2、数据类型3、初始化(构造张量)4、常用操作5、常用属性5.1 存储(st

windows系统下shutdown重启关机命令超详细教程

《windows系统下shutdown重启关机命令超详细教程》shutdown命令是一个强大的工具,允许你通过命令行快速完成关机、重启或注销操作,本文将为你详细解析shutdown命令的使用方法,并提... 目录一、shutdown 命令简介二、shutdown 命令的基本用法三、远程关机与重启四、实际应用

Python 中 requests 与 aiohttp 在实际项目中的选择策略详解

《Python中requests与aiohttp在实际项目中的选择策略详解》本文主要介绍了Python爬虫开发中常用的两个库requests和aiohttp的使用方法及其区别,通过实际项目案... 目录一、requests 库二、aiohttp 库三、requests 和 aiohttp 的比较四、requ

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情

Python在固定文件夹批量创建固定后缀的文件(方法详解)

《Python在固定文件夹批量创建固定后缀的文件(方法详解)》文章讲述了如何使用Python批量创建后缀为.md的文件夹,生成100个,代码中需要修改的路径、前缀和后缀名,并提供了注意事项和代码示例,... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果5.