C#實(shí)現(xiàn)簡(jiǎn)易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)
一、效果展示
二、隨機(jī)生成熱力點(diǎn)
熱力點(diǎn)類(lèi)
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; } }
隨機(jī)生成熱力點(diǎn)
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; } //此方法用于在繪圖表面上繪制實(shí)際的徑向漸變“點(diǎn)”。這可能是整個(gè)項(xiàng)目中最重要的方法,因?yàn)樗梢蕴幚聿煌笮『兔芏鹊睦L圖點(diǎn)。 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) { // 在定義半徑的圓的圓周上繪制新點(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); // 計(jì)算比例以將字節(jié)強(qiáng)度范圍從 0-255 縮放到 0-1 float fRatio = 1F / Byte.MaxValue; // 預(yù)計(jì)算字節(jié)最大值的一半 byte bHalf = Byte.MaxValue / 2; // 將其中心值的強(qiáng)度從低高翻轉(zhuǎn)到高低 int iIntensity = (byte)(HeatPoint.Intensity - ((HeatPoint.Intensity - bHalf) * 2)); // 存儲(chǔ)縮放和翻轉(zhuǎn)的強(qiáng)度值以用于梯度中心位置 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 以使用圓周點(diǎn)創(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)建一組顏色映射以將我們的灰度蒙版重新映射為全色 // 接受一個(gè) alpha 字節(jié)來(lái)指定輸出圖像的透明度 ColorMap[] Colors = CreatePaletteIndex(Alpha); // 創(chuàng)建新的圖像屬性類(lèi)來(lái)處理顏色重新映射 // 注入我們的顏色映射數(shù)組來(lái)指示圖像屬性類(lèi)如何進(jìn)行著色 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; }
五、源碼下載
到此這篇關(guān)于C#實(shí)現(xiàn)簡(jiǎn)易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)的文章就介紹到這了,更多相關(guān)C#? 灰度圖和熱力圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c# Newtonsoft 六個(gè)值得使用的特性(上)
這篇文章主要介紹了c# Newtonsoft 六個(gè)值得使用的特性,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06Windows Form 分頁(yè) 具體實(shí)現(xiàn)
其實(shí)功能實(shí)現(xiàn)很簡(jiǎn)單。我做的是一個(gè)通用的分頁(yè)控件。項(xiàng)目時(shí)間很緊,可能有點(diǎn)粗糙。歡迎大家斧正。不說(shuō)了直接貼代碼吧2013-12-12C#實(shí)現(xiàn)單例模式的6種方法小結(jié)
這篇文章主要介紹了C#實(shí)現(xiàn)單例模式的6種方法,C#中實(shí)現(xiàn)單例有很多種方法,本文將按順序介紹非線(xiàn)程安全、完全懶漢式、線(xiàn)程安全和低/高性能集中版本,需要的朋友可以參考下2022-09-09C#語(yǔ)言基礎(chǔ)——結(jié)構(gòu)體和枚舉類(lèi)型全面解析
下面小編就為大家?guī)?lái)一篇C#語(yǔ)言基礎(chǔ)——結(jié)構(gòu)體和枚舉類(lèi)型全面解析。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07WinForm實(shí)現(xiàn)自定義右下角提示效果的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)自定義右下角提示效果的方法,涉及WinForm自定義提示效果的實(shí)現(xiàn)方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08c#中string的特性介紹及注意事項(xiàng)小結(jié)
這篇文章主要給大家介紹了關(guān)于c#中string的特性介紹及注意事項(xiàng)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11DevExpress之ChartControl實(shí)現(xiàn)餅狀圖百分比演示實(shí)例
這篇文章主要介紹了DevExpress之ChartControl實(shí)現(xiàn)餅狀圖百分比演示的方法,實(shí)例講述了窗體與圖形繪制函數(shù)的用法,需要的朋友可以參考下2014-10-10