C#实现简易灰度图和酷炫HeatMap热力图winform(附DEMO)
程序员文章站
2022-03-16 19:20:35
目录一、效果展示二、随机生成热力点三、灰度图生成解析四、热力图生成解析五、源码下载一、效果展示二、随机生成热力点热力点类 class heatpoint { public i...
一、效果展示
二、随机生成热力点
热力点类
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<heatpoint> aheatpoints) { 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<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); // 计算比例以将字节强度范围从 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(附demo)的文章就介绍到这了,更多相关c# 灰度图和热力图内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: css背景和边框标签实例详解