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


二、漸進(jìn)顏色調(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;
// 遍歷每個(gè)像素并創(chuàng)建一個(gè)新的顏色映射
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>
/// 獲得兩個(gè)顏色之間漸進(jìn)顏色的集合
/// </summary>
/// <param name="sourceColor">起始顏色</param>
/// <param name="destColor">終止顏色</param>
/// <param name="count">漸進(jìn)顏色的個(gè)數(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;
}
三、熱力點(diǎn)大小和擴(kuò)展大小
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)
{
// 在定義半徑的圓的圓周上繪制新點(diǎn)
// 使用點(diǎn)坐標(biāo)、半徑和角度
// 計(jì)算這個(gè)迭代點(diǎn)在圓上的位置
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)建新的顏色混合來(lái)告訴 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 以使用圓周點(diǎn)創(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);
//清除整個(gè)繪圖面并以白色填充
graphics.Clear(System.Drawing.Color.White);
foreach (HeatPoint point in aHeatPoints)
{
DrawHeatPoint2(graphics, point);
}
return bitmap;
}
到此這篇關(guān)于C# .Net實(shí)現(xiàn)灰度圖和HeatMap熱力圖winform(進(jìn)階)的文章就介紹到這了,更多相關(guān)C# .Net 灰度圖 熱力圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows系統(tǒng)中使用C#編寫(xiě)藍(lán)牙通信程序的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Windows系統(tǒng)中使用C#編寫(xiě)藍(lán)牙通信程序的簡(jiǎn)單實(shí)例,文中的例子使用到了32feet.NET中的InTheHand.Net.Personal類(lèi)庫(kù),需要的朋友可以參考下2016-04-04
C#使用文件流FileStream和內(nèi)存流MemoryStream操作底層字節(jié)數(shù)組byte[]
這篇文章介紹了C#使用文件流FileStream和內(nèi)存流MemoryStream操作底層字節(jié)數(shù)組byte[]的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C# 10分鐘完成百度人臉識(shí)別(入門(mén)篇)
這篇文章主要介紹了C# 10分鐘完成百度人臉識(shí)別(入門(mén)篇),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
C#實(shí)現(xiàn)簡(jiǎn)單的RSA非對(duì)稱(chēng)加密算法示例
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單的RSA非對(duì)稱(chēng)加密算法,結(jié)合實(shí)例形式分析了C#實(shí)現(xiàn)RSA加密的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
C#實(shí)現(xiàn)簡(jiǎn)易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)
本文主要介紹了C#實(shí)現(xiàn)簡(jiǎn)易灰度圖和酷炫HeatMap熱力圖winform(附DEMO),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
測(cè)試框架nunit之a(chǎn)ssertion斷言使用詳解
NUnit是.Net平臺(tái)的測(cè)試框架,廣泛用于.Net平臺(tái)的單元測(cè)試和回歸測(cè)試中,下面我們用示例詳細(xì)學(xué)習(xí)一下他的使用方法2014-01-01

