簡體   English   中英

c#wpf多邊形到位圖不顯示任何內容

[英]c# wpf polygon to bitmap not displaying anything

抱歉,如果您認為此問題已得到回答,那么我確實到處都在試圖找出執行此操作的結果,但它沒有顯示任何內容。 這是我的全部代碼:

Polygon hexagon = new Polygon();
PointCollection pc = new PointCollection();
double side = 25;
double xOffset = 0, yOffset = 0;
double r = System.Math.Cos((System.Math.PI / 180) * 30) * side;
double h = System.Math.Sin((System.Math.PI / 180) * 30) * side;

//Create the 6 points needed to create a hexagon
pc.Add(new Point(xOffset, yOffset)); //Point 1
pc.Add(new Point(xOffset + side, yOffset)); //Point 2
pc.Add(new Point(xOffset + side + h, yOffset + r)); //Point 3
pc.Add(new Point(xOffset + side, yOffset + 2 * r)); //Point 4
pc.Add(new Point(xOffset, yOffset + 2 * r)); //Point 5
pc.Add(new Point(xOffset - h, yOffset + r)); //Point 6

hexagon.Stroke = Brushes.Blue;
hexagon.StrokeThickness = 1;
hexagon.Fill = Brushes.LightBlue;
hexagon.Points = pc;

RenderTargetBitmap bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Default);
bmp.Render(hexagon);
img.Source = bmp;

我創建了一個六邊形作為多邊形對象,並使用RenderTargetBitmap嘗試將多邊形轉換為位圖,但似乎沒有顯示任何可見的內容。 我還添加了一個畫布,並在其中添加了Polygon對象,這似乎可行。 只是在轉換為位圖時。 我確實對代碼中的錯誤感到迷茫。 我現在在主窗口已加載事件中擁有所有內容。

幫助將不勝感激,謝謝。

解決方案可能很簡單,或者我忽略了一些,希望解決方案很簡單:)。

WPF UIElement必須先布局才能可見。 它必須至少獲得一個Measure and Arrange調用,並在其中獲得一個可用的Size和一個最后的Rect Rect(通常從其父面板)。 在將新創建的元素渲染到RenderTargetBitmap中時,您將使用適當的Size和Rect值手動調用這些方法:

...
hexagon.Measure(new Size(200, 200)); // adjust this to your needs
hexagon.Arrange(new Rect(0, 0, 200, 200)); // adjust this to your needs

var bmp = new RenderTargetBitmap(200, 200, 96, 96, PixelFormats.Default);
bmp.Render(hexagon);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM