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

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.xdnf.cn/news/16691.html

相关文章:

  • Conda环境下配置的基本命令
  • Ubuntu 下配置 NVIDIA 驱动与 CUDA 环境(适配 RTX 4060Ti)
  • webpack-babel
  • SAM附录详解
  • 【C#】基于SharpCompress实现压缩包解压功能
  • 揭秘动态测试:软件质量的实战防线
  • 【Golang】用官方rate包构造简单IP限流器
  • 【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 微博评论数据可视化分析-点赞区间折线图实现
  • 04百融云策略引擎项目laravel实战步完整安装composer及tcpdf依赖库和验证-优雅草卓伊凡
  • Cesium 快速入门(二)底图更换
  • 数据库学习------数据库隔离类型及其与事务特性
  • 如何将 Redis 监控集成到微服务整体的监控体系中( 如 Prometheus + Grafana)
  • 如何为C#加入EPPlus 包
  • 哈希相关的模拟实现
  • 【人工智能】当AI智能体遇上安全与伦理:一场技术与人性的对话
  • Java学习第九十一部分——OkHttp
  • Unity游戏开发中的3D数学基础详解
  • SQL 中 WHERE 与 HAVING 的用法详解:分组聚合场景下的混用指南
  • Kotlin -> 普通Lambda vs 挂起Lambda
  • Side band ECC、Inline ECC、On-die ECC、Link ECC
  • Jinja2 详细讲解
  • 基于32nm CMOS工艺的传输门D触发器设计及HSPICE仿真分析
  • 三坐标测量仪攻克深孔检测!破解新能源汽车阀体阀孔测量难题
  • 电子电气架构 --- 车载48V系统开辟全新道路
  • React组件化的封装
  • 【Kiro Code】Chat 聊天功能
  • Amazon Aurora MySQL 8.0 完整指南
  • 网络爬虫(python)入门
  • 安卓基础布局核心知识点整理
  • 嵌入式 C 语言入门:循环结构学习笔记 —— 从语法到实用技巧