您好,登錄后才能下訂單哦!
本篇內容介紹了“C# .Net如何實現灰度圖和HeatMap熱力圖winform”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
上一個圖是之前的效果,下一個圖是改進后的效果
//創建調色板,顏色映射 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)); //顏色調色板展示 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; // 遍歷每個像素并創建一個新的顏色映射 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">漸進顏色的個數</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) { // 在定義半徑的圓的圓周上繪制新點 // 使用點坐標、半徑和角度 // 計算這個迭代點在圓上的位置 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); 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) }; // 創建新的 PathGradientBrush 以使用圓周點創建徑向漸變 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; }
“C# .Net如何實現灰度圖和HeatMap熱力圖winform”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。