溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C#如何實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform

發(fā)布時間:2021-12-13 14:10:20 來源:億速云 閱讀:239 作者:柒染 欄目:開發(fā)技術(shù)

C#如何實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform,相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

一、效果展示

C#如何實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform

C#如何實現(xiàn)簡易灰度圖和酷炫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);
    }


    // 創(chuàng)建新的顏色混合來告訴 PathGradientBrush 使用什么顏色以及放置它們的位置
    ColorBlend colorBlend = new ColorBlend(3);

    // 計算比例以將字節(jié)強度范圍從 0-255 縮放到 0-1
    float fRatio = 1F / Byte.MaxValue;
    // 預計算字節(jié)最大值的一半
    byte bHalf = Byte.MaxValue / 2;
    // 將其中心值的強度從低高翻轉(zhuǎn)到高低
    int iIntensity = (byte)(HeatPoint.Intensity - ((HeatPoint.Intensity - bHalf) * 2));
    // 存儲縮放和翻轉(zhuǎn)的強度值以用于梯度中心位置
    float fIntensity = iIntensity * fRatio;
    // 定義漸變顏色的位置,使用intesity將中間顏色調(diào)整為
    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)
    };

    // 創(chuàng)建新的 PathGradientBrush 以使用圓周點創(chuàng)建徑向漸變
    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);

    // 構(gòu)建一組顏色映射以將我們的灰度蒙版重新映射為全色
    // 接受一個 alpha 字節(jié)來指定輸出圖像的透明度
    ColorMap[] Colors = CreatePaletteIndex(Alpha);

    // 創(chuàng)建新的圖像屬性類來處理顏色重新映射
    // 注入我們的顏色映射數(shù)組來指示圖像屬性類如何進行著色
    ImageAttributes Remapper = new ImageAttributes();
    Remapper.SetRemapTable(Colors);

    // 使用新的顏色映射方案將我們的蒙版繪制到我們的內(nèi)存位圖工作表面上
    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;
}

看完上述內(nèi)容,你們掌握C#如何實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform的方法了嗎?如果還想學到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI