欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# .Net实现灰度图和HeatMap热力图winform(进阶)

程序员文章站 2022-03-06 17:41:15
目录一、前文二、渐进颜色调色板三、热力点大小和扩展大小一、前文前文可以参考我的前一篇博文:c# .net实现简易灰度图和酷炫heatmap热力图winform但是,先前的热力图效果,我并不满意。不满意...

一、前文

前文可以参考我的前一篇博文:c# .net实现简易灰度图和酷炫heatmap热力图winform

但是,先前的热力图效果,我并不满意。不满意的地方主要有三点:

  • 热力图的颜色是通过一个调色板图片来实现,如果想要其他颜色,改起来比较麻烦
  • 热力图的扩散效果不好看,不够渐进
  • 热力图的每个点大小都一致,应该是大小不一才对

因此,我做了改进,上一个图是之前的效果,下一个图是改进后的效果

C# .Net实现灰度图和HeatMap热力图winform(进阶)

C# .Net实现灰度图和HeatMap热力图winform(进阶)

二、渐进颜色调色板

//创建调色板,颜色映射
private colormap[] createpalette()
{
    colormap[] colormaps = new colormap[256];
    list<color> newcolors = new list<color>();

    //颜色集合
    newcolors.addrange(getgradientcolorlist(color.red, color.yellow, 64));
    newcolors.addrange(getgradientcolorlist(color.yellow, color.green, 64));
    newcolors.addrange(getgradientcolorlist(color.green, color.blue, 64));
    newcolors.addrange(getgradientcolorlist(color.blue, color.navy, 64));
    //颜色调色板展示
    bitmap colorbitmap = new bitmap(colorpanel.width, colorpanel.height);

    graphics graphic = graphics.fromimage(colorbitmap);             
    for (int i = 0; i < 256; i++)
    {
        solidbrush solidbrush = new solidbrush(newcolors[i]);
        rectangle rectangle = new rectangle((int)(i * 2), 0, (int)2, colorpanel.height);
        graphic.fillrectangle(solidbrush, rectangle);
        graphic.save();
        solidbrush.dispose();
    }
    colorpanel.backgroundimage = colorbitmap;

    // 遍历每个像素并创建一个新的颜色映射
    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, newcolors[x]);
    }
    return colormaps;
}

/// <summary>
/// 获得两个颜色之间渐进颜色的集合
/// </summary>
/// <param name="sourcecolor">起始颜色</param>
/// <param name="destcolor">终止颜色</param>
/// <param name="count">渐进颜色的个数</param>
/// <returns>返回颜色集合</returns>
public static list<color> getgradientcolorlist(color srccolor, color descolor, int count)
{
    list<color> colorfactorlist = new list<color>();
    int redspan = descolor.r - srccolor.r;
    int greenspan = descolor.g - srccolor.g;
    int bluespan = descolor.b - srccolor.b;
    for (int i = 0; i < count; i++)
    {
        color color = color.fromargb(
            srccolor.r + (int)((double)i / count * redspan),
            srccolor.g + (int)((double)i / count * greenspan),
            srccolor.b + (int)((double)i / count * bluespan)
        );
        colorfactorlist.add(color);
    }
    return colorfactorlist;
}

三、热力点大小和扩展大小

private void drawheatpoint2(graphics graphics, heatpoint heatpoint)
{
    console.writeline("heatpoint.intensity = " + heatpoint.intensity);

    int radius = 40 * (heatpoint.intensity+6) / 240;

    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());
    //brush.dispose();
}

四、更新视图 

private void updateview()
{
    //灰度
    bitmap bitmap1 = createintensitymask(new bitmap((int)panel1.width, (int)panel1.height, system.drawing.imaging.pixelformat.format32bppargb), heatpoints, 1);
    panel1.backgroundimage = bitmap1;

    //上色
    panel3.backgroundimage = colorize(bitmap1);
}


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)
    {
       drawheatpoint2(graphics, point);
    }
    return bitmap;
} 

到此这篇关于c# .net实现灰度图和heatmap热力图winform(进阶)的文章就介绍到这了,更多相关c# .net 灰度图 热力图内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!