【学习笔记】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

相关文章

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

MyBatis常用XML语法详解

《MyBatis常用XML语法详解》文章介绍了MyBatis常用XML语法,包括结果映射、查询语句、插入语句、更新语句、删除语句、动态SQL标签以及ehcache.xml文件的使用,感兴趣的朋友跟随小... 目录1、定义结果映射2、查询语句3、插入语句4、更新语句5、删除语句6、动态 SQL 标签7、ehc

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Python版本信息获取方法详解与实战

《Python版本信息获取方法详解与实战》在Python开发中,获取Python版本号是调试、兼容性检查和版本控制的重要基础操作,本文详细介绍了如何使用sys和platform模块获取Python的主... 目录1. python版本号获取基础2. 使用sys模块获取版本信息2.1 sys模块概述2.1.1

一文详解Python如何开发游戏

《一文详解Python如何开发游戏》Python是一种非常流行的编程语言,也可以用来开发游戏模组,:本文主要介绍Python如何开发游戏的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、python简介二、Python 开发 2D 游戏的优劣势优势缺点三、Python 开发 3D

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Java中的.close()举例详解

《Java中的.close()举例详解》.close()方法只适用于通过window.open()打开的弹出窗口,对于浏览器的主窗口,如果没有得到用户允许是不能关闭的,:本文主要介绍Java中的.... 目录当你遇到以下三种情况时,一定要记得使用 .close():用法作用举例如何判断代码中的 input