当前位置: 首页 > news >正文

Winform PathGradientBrush类使用

 PathGradientBrush

用于创建路径(或形状)渐变填充的画刷;

用渐变的效果填充图形,渐变的方向是从由路径定义的图形边界指向图形的中心。

PathGradientBrush的父类是Brush:

用来填充图形(如形状或文本)内部区域的对象。在.NET框架中,画刷是System.Drawing命名空间的一部分,通常用于GDI+绘图操作。

使用效果:

在winform中生成九圆阵列,每一个圆就是一个由PathGradientBrush填充的图案。

准备:

需要一个panel控件承载Bitmap,实际上是在Bitmap上画图案。比较简单这里不展示。

需要九个圆的位置,代码:
 

 public List<HeatPoint> GetTestPointList(){// 参数配置int startX = 30;      // 起点Xint startY = 30;      // 起点Yint spacing = 100;    // 点间距int rows = 3;        // 行数int cols = 3;        // 列数return  GeneratePointGrid(startX, startY, spacing, rows, cols);}/// <summary>/// 生成均匀点阵/// </summary>public  List<HeatPoint> GeneratePointGrid(int startX, int startY, int spacing, int rows, int cols){List<HeatPoint> grid = new List<HeatPoint>();for (int row = 0; row < rows; row++){for (int col = 0; col < cols; col++){int x = startX + col * spacing;int y = startY + row * spacing;grid.Add(new HeatPoint(x, y));}}return grid;}

生成:

 Bitmap bitmap1 = CreateIntensityMask(new Bitmap((int)panel1.Width, (int)panel1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb), HeatPoints);
 private Bitmap CreateIntensityMask(Bitmap bitmap, List<HeatPoint> aHeatPoints){//从Bitmap获得Graphics GDI+ 绘图图面Graphics graphics = Graphics.FromImage(bitmap);//清除整个绘图面并以白色填充graphics.Clear(System.Drawing.Color.White);//绘制图案foreach (HeatPoint point in aHeatPoints){DrawHeatPoint(graphics, point);}return bitmap;}

 

 private void DrawHeatPoint(Graphics graphics, HeatPoint heatPoint){//半径int radius = 20;List<System.Drawing.Point> pointsList = new List<System.Drawing.Point>();for (double degrees = 0; degrees <= 360; degrees += 10){// 在定义半径的圆的圆周上绘制新点// 使用点坐标、半径和角度// 计算这个迭代点在圆上的位置System.Drawing.Point point = new System.Drawing.Point();point.X = Convert.ToInt32(heatPoint.X + radius * Math.Cos((Math.PI / 180) * degrees));point.Y = Convert.ToInt32(heatPoint.Y + radius * Math.Sin((Math.PI / 180) * degrees));pointsList.Add(point);}// 创建新的颜色混合来告诉 PathGradientBrush 使用什么颜色以及放置它们的位置ColorBlend colorBlend = new ColorBlend(3);colorBlend.Positions = new float[3] { 0, 0.8f, 1 };colorBlend.Colors = new System.Drawing.Color[3]{System.Drawing.Color.FromArgb(0, System.Drawing.Color.White),System.Drawing.Color.FromArgb(heatPoint.Intensity, System.Drawing.Color.Black),System.Drawing.Color.FromArgb(heatPoint.Intensity, System.Drawing.Color.Black)};// 创建新的 PathGradientBrush 以使用圆周点创建径向渐变PathGradientBrush brush = new PathGradientBrush(pointsList.ToArray());// 将颜色混合传递给 PathGradientBrush 以指示它如何生成渐变brush.InterpolationColors = colorBlend;graphics.FillPolygon(brush, pointsList.ToArray());}

其中ColorBlend 类指定渐变效果。 

顺带看一下graphics.FillPolygon(brush, pointsList.ToArray())中的pointsList是怎样分布的:

定义一个新的panel,编写一个新方法:

public void DrawSonPoints(List<System.Drawing.Point> pointsList)
{Bitmap bitmap = new Bitmap((int)panel2.Width, (int)panel2.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);Graphics graphics = Graphics.FromImage(bitmap);System.Drawing.Brush brush = new SolidBrush(Color.FromArgb(0, 255, 0));//设置画刷的颜色为绿色foreach (var point in pointsList){graphics.FillEllipse(brush, point.X, point.Y, 2, 2); //画一个椭圆,并用绿色填充}panel2.BackgroundImage = bitmap;}

 

定义一个新属性:public List<System.Drawing.Point> PointsList = new List<Point>(); 

在绘图方法里接收路径点位集合,然后调用 DrawSonPoints呈现:

 

 

相关类:

 public class HeatPoint{public int X;public int Y;public byte Intensity;public HeatPoint(int iX, int iY, byte bIntensity){X = iX;Y = iY;Intensity = bIntensity;}public HeatPoint(int iX, int iY){X = iX;Y = iY;}}

参考:

 C# .Net实现简易灰度图和酷炫HeatMap热力图winform(进阶)_c# 热力图-CSDN博客

C#学习笔记:GDI图形高级编程(2)——关于Brush类_c# brush-CSDN博客

 C# GDI+编程(一)_c# colorblend-CSDN博客

http://www.dtcms.com/a/308594.html

相关文章:

  • C#程序本地运行正常,通过网络下载报错:FileLoadException:“未能加载文件或程序集“xxx.dll”或它的某一个依赖项。
  • 【pycharm的使用】
  • Qwen3-30B-A3B-Thinking-2507 推理模型深度评测
  • 250721脑电分析课题进展——手工特征总结
  • lsof命令
  • SSO面临的问题
  • 为什么有时神经元会输出类似(甚至一样)?
  • 行业分享丨从工具应用到体系进化:东风商用车仿真体系建设与实践
  • 【源力觉醒 创作者计划】文心一言与deepseek集成springboot开发哪个更方便
  • 【力扣】面试经典150题总结01-数组/字符串
  • Dev-C++ 6.3 安装与使用指南:适合新手的C/C++编程工具
  • Allegro实用技巧-Snap-命令行移动
  • Android端RTMP低延迟播放器在工业与智能场景下的架构与落地
  • MySQL 中 CHAR 和 VARCHAR 类型有什么区别?
  • 一次性接收大量上传图片,后端优化方式
  • 【Git】Git 实战:完整拉取项目所有分支和标签,切换远程仓库,解决保护分支推送冲突
  • Linux Flathub软件管理方法 使用指南
  • 搭建个人博客
  • 决策树实现回归任务
  • 利用可观测性进行高效故障治理:从预防到改进的完整实践
  • 从Excel到工时管理系统:企业如何选择更高效的工时记录工具?
  • 第二十九章:AI的“原子与批次”:高维数据表示与操作精炼【总结前面(1)】
  • Windows 安全中心是什么?如何关闭 Windows 11 的安全中心
  • 算法导论第三版代码python实现与部分习题答案-第六章:堆排序
  • DooTask非营利性组织:让高效协作触手可及
  • Day 5: 深度学习理论与PyTorch实现 - 神经网络训练的艺术
  • RocketMQ消息队列:从入门到Spring Boot实战
  • 【React】fiber 架构
  • OS架构整理
  • Spring Boot音乐服务器项目-移除喜欢和操作