C# .Net實現(xiàn)灰度圖和HeatMap熱力圖winform(進階)
一、前文
前文可以參考我的前一篇博文:C# .Net實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform
但是,先前的熱力圖效果,我并不滿意。不滿意的地方主要有三點:
- 熱力圖的顏色是通過一個調(diào)色板圖片來實現(xiàn),如果想要其他顏色,改起來比較麻煩
- 熱力圖的擴散效果不好看,不夠漸進
- 熱力圖的每個點大小都一致,應(yīng)該是大小不一才對
因此,我做了改進,上一個圖是之前的效果,下一個圖是改進后的效果


二、漸進顏色調(diào)色板
//創(chuàng)建調(diào)色板,顏色映射
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));
//顏色調(diào)色板展示
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;
// 遍歷每個像素并創(chuàng)建一個新的顏色映射
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">漸進顏色的個數(shù)</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)
{
// 在定義半徑的圓的圓周上繪制新點
// 使用點坐標(biāo)、半徑和角度
// 計算這個迭代點在圓上的位置
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);
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)
};
// 創(chuàng)建新的 PathGradientBrush 以使用圓周點創(chuàng)建徑向漸變
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;
}
到此這篇關(guān)于C# .Net實現(xiàn)灰度圖和HeatMap熱力圖winform(進階)的文章就介紹到這了,更多相關(guān)C# .Net 灰度圖 熱力圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows系統(tǒng)中使用C#編寫藍牙通信程序的簡單實例
這篇文章主要介紹了Windows系統(tǒng)中使用C#編寫藍牙通信程序的簡單實例,文中的例子使用到了32feet.NET中的InTheHand.Net.Personal類庫,需要的朋友可以參考下2016-04-04
C#使用文件流FileStream和內(nèi)存流MemoryStream操作底層字節(jié)數(shù)組byte[]
這篇文章介紹了C#使用文件流FileStream和內(nèi)存流MemoryStream操作底層字節(jié)數(shù)組byte[]的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
C#實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)
本文主要介紹了C#實現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
測試框架nunit之a(chǎn)ssertion斷言使用詳解
NUnit是.Net平臺的測試框架,廣泛用于.Net平臺的單元測試和回歸測試中,下面我們用示例詳細(xì)學(xué)習(xí)一下他的使用方法2014-01-01

