温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#如何实现简易灰度图和酷炫HeatMap热力图winform

发布时间:2021-12-13 14:10:20 来源:亿速云 阅读:215 作者:柒染 栏目:开发技术

C#如何实现简易灰度图和酷炫HeatMap热力图winform,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

一、效果展示

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, 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的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI