- 一、效果展示
- 二、随机生成热力点
- 三、灰度图生成解析
- 四、热力图生成解析
- 五、更新视图
- 六、源码下载
- 七、参考
- 八、更新了一版更强的
热力点类
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, List四、热力图生成解析aHeatPoints) { //从Bitmap获得Graphics GDI+ 绘图图面 Graphics graphics = Graphics.FromImage(bitmap); //清除整个绘图面并以白色填充 graphics.Clear(System.Drawing.Color.White); foreach (HeatPoint point in aHeatPoints) { 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)
{
//创建新的位图,为上色做准备
Bitmap output = new Bitmap(mask.Width, mask.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//同理,从Bitmap获得Graphics GDI+ 绘图图面
Graphics graphics = Graphics.FromImage(output);
//清除整个绘图面并以透明填充
graphics.Clear(System.Drawing.Color.Transparent);
// 构建一组颜色映射以将我们的灰度蒙版重新映射为全色
ColorMap[] colorMaps = CreatePalette();
// 创建新的图像属性类来处理颜色重新映射
// 注入我们的颜色映射数组来指示图像属性类如何进行着色
ImageAttributes Remapper = new ImageAttributes();
Remapper.SetRemapTable(colorMaps);
// 使用新的颜色映射方案将我们的蒙版绘制到我们的内存位图工作表面上
graphics.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[] CreatePalette()
{
ColorMap[] colorMaps = new ColorMap[256];
//获取调色板图像
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream("热力图Demo.Image.gradient-palette.jpg");
Bitmap palette = new Bitmap(stream);
// 遍历每个像素并创建一个新的颜色映射
for (int X = 0; X <= 255; X++)
{
colorMaps[X] = new ColorMap();
colorMaps[X].OldColor = System.Drawing.Color.FromArgb(X, X, X);
colorMaps[X].NewColor = System.Drawing.Color.FromArgb(255, palette.GetPixel(X, 0));
}
return colorMaps;
}
五、更新视图
private void UpdateView()
{
//灰度
bMap = CreateIntensityMask(bMap, heatPoints);
panel1.BackgroundImage = bMap;
//上色
bMap = Colorize(bMap);
panel2.BackgroundImage = bMap;
}
六、源码下载
热力图Demo.zip
七、参考Creating Heat Maps with C# .NET
八、更新了一版更强的C# .Net实现简易灰度图和酷炫HeatMap热力图winform(进阶)
觉得好,就一键三连呗(点赞+收藏+关注)



