問題描述
抱歉,如果您認為這個問題已經得到解答,我確實到處尋找,試圖找出這樣做的原因,但它沒有顯示任何內容.這是我所有的代碼:
Sorry if you think this question was already answered, I did look everywhere trying to find out how come when I do this, it is not displaying anything. This is all my code:
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 對象,這似乎有效.只是在轉換為位圖時.我真的不知道我的代碼有什么問題.我現在在主窗口加載事件中擁有一切.
I created a hexagon as a polygon object and I used RenderTargetBitmap to try to convert the polygon to a bitmap but it does not seem to be displaying anything that I can see. I have also added a canvas and added the Polygon object there and that seems to work. It is just when converting to a bitmap. I really am at a lost as to what is wrong in my code. I have everything right now in the main windows loaded event.
將不勝感激,謝謝.
解決方案可能很簡單或者我忽略了一些東西,希望解決方案很簡單:)
The solution may be simple or something I overlooked, hopefully the solution is simple :).
推薦答案
WPF UIElement 必須在可見之前進行布局.它必須至少獲得一個 Measure
和 Arrange
調用,在那里它獲得一個可用的 Size 和一個最終的排列 Rect(通常來自它的父面板).將新創建的元素渲染到 RenderTargetBitmap 中時,您將手動調用這些方法,并為 Size 和 Rect 設置適當的值:
A WPF UIElement has to be laid out before being visible. It has to get at least one Measure
and Arrange
call, where it gets an available Size and a final arrange Rect (usually from its parent Panel). When rendering a newly created element into a RenderTargetBitmap, you would call these methods manually, with appropriate values for the Size and 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);
這篇關于c# wpf 多邊形到位圖不顯示任何內容的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!