c# wpf polygon to bitmap not displaying anything(c# wpf 多边形到位图不显示任何内容)
问题描述
抱歉,如果您认为这个问题已经得到解答,我确实到处寻找,试图找出这样做的原因,但它没有显示任何内容.这是我所有的代码:
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 多边形到位图不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:c# wpf 多边形到位图不显示任何内容
基础教程推荐
- 创建属性设置器委托 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01