c# wpf 多边形到位图不显示任何内容
- 作者: 用户38296819
- 来源: 51数据库
- 2022-10-20
问题描述
抱歉,如果您认为这个问题已经得到解答,我确实到处寻找,试图找出这样做的原因,但它没有显示任何内容.这是我所有的代码:
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#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
