C#如何实现简易灰度图和酷炫HeatMap热力图winform
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,C#如何实现简易灰度图和酷炫HeatMap热力图winform,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、效果展示二、随机生成热力
千家信息网最后更新 2026年11月03日C#如何实现简易灰度图和酷炫HeatMap热力图winform
C#如何实现简易灰度图和酷炫HeatMap热力图winform,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
一、效果展示

二、随机生成热力点
热力点类
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; } }随机生成热力点
privatevoid generateBtn_Click(object sender, EventArgs e){ Random rRand = new Random(); for (int i = 0; i < 500; i++) { int iX = rRand.Next(0, 800); int iY = rRand.Next(0, 800); byte iIntense = (byte)rRand.Next(0, 180); heatPoints.Add(new HeatPoint(iX, iY, iIntense)); } UpdateView();}三、灰度图生成解析
private Bitmap CreateIntensityMask(Bitmap bitmap, ListaHeatPoints){ Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(System.Drawing.Color.White); foreach (HeatPoint point in aHeatPoints) { if (point.Intensity * 30 / 180 == 0) continue; DrawHeatPoint(graphics, point, point.Intensity * 30 / 180); //DrawHeatPoint(graphics, point, 15); } return bitmap;}//此方法用于在绘图表面上绘制实际的径向渐变"点"。这可能是整个项目中最重要的方法,因为它可以处理不同大小和密度的绘图点。private void DrawHeatPoint(Graphics graphics, HeatPoint HeatPoint, int radius){ List pointsList = new List (); 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); // 计算比例以将字节强度范围从 0-255 缩放到 0-1 float fRatio = 1F / Byte.MaxValue; // 预计算字节最大值的一半 byte bHalf = Byte.MaxValue / 2; // 将其中心值的强度从低高翻转到高低 int iIntensity = (byte)(HeatPoint.Intensity - ((HeatPoint.Intensity - bHalf) * 2)); // 存储缩放和翻转的强度值以用于梯度中心位置 float fIntensity = iIntensity * fRatio; // 定义渐变颜色的位置,使用intesity将中间颜色调整为 colorBlend.Positions = new float[3] { 0, fIntensity, 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());}
四、热力图生成解析
public static Bitmap Colorize(Bitmap Mask, byte Alpha){ Bitmap Output = new Bitmap(Mask.Width, Mask.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics Surface = Graphics.FromImage(Output); Surface.Clear(System.Drawing.Color.Transparent); // 构建一组颜色映射以将我们的灰度蒙版重新映射为全色 // 接受一个 alpha 字节来指定输出图像的透明度 ColorMap[] Colors = CreatePaletteIndex(Alpha); // 创建新的图像属性类来处理颜色重新映射 // 注入我们的颜色映射数组来指示图像属性类如何进行着色 ImageAttributes Remapper = new ImageAttributes(); Remapper.SetRemapTable(Colors); // 使用新的颜色映射方案将我们的蒙版绘制到我们的内存位图工作表面上 Surface.DrawImage(Mask, new System.Drawing.Rectangle(0, 0, Mask.Width, Mask.Height), 0, 0, Mask.Width, Mask.Height, GraphicsUnit.Pixel, Remapper); return Output;}private static ColorMap[] CreatePaletteIndex(byte Alpha){ ColorMap[] OutputMap = new ColorMap[256]; Assembly myAssembly = Assembly.GetExecutingAssembly(); Stream myStream = myAssembly.GetManifestResourceStream("热力图Demo.Image.gradient-palette.jpg"); Bitmap Palette = new Bitmap(myStream); for (int X = 0; X <= 255; X++) { OutputMap[X] = new ColorMap(); OutputMap[X].OldColor = System.Drawing.Color.FromArgb(X, X, X); OutputMap[X].NewColor = System.Drawing.Color.FromArgb(Alpha, Palette.GetPixel(X, 0)); } return OutputMap;}看完上述内容,你们掌握C#如何实现简易灰度图和酷炫HeatMap热力图winform的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
颜色
生成
灰度
位置
图像
字节
强度
方法
热力
简易
C#
内容
半径
圆周
属性
径向
指示
更多
表面
问题
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技名片
广州百单互联网科技有限公司
互联网的科技
互联网科技深圳
猪兼强互联网科技
互联网 卓越科技
广州宏创互联网科技
北京禾田雨橡互联网科技
奔汉互联网科技
滨湖区互联网智能科技产品供应
青海省互联网新科技
科技赋能互联网健康
科技与互联网哪个更好
岳阳华臻互联网科技有限公司
智腾互联网科技有限公司
电子科技大学互联网创业大赛
济南有互联网科技公司吗
互联网高科技行业有哪些企业
互联网金融金融科技
现代科技的标志之一的互联网
互联网高科技
善班互联网科技有限公司
天速互联网络科技有限公司
互联网教育科技公司vipkid
互联网金融中的现代科技
互联网5g黑科技有哪些
互联网安防科技感
宁波跨境商互联网科技有限公司
互联网领先科技活动
六子台科技互联网展会