C#WPF多边形为位图不显示任何内容 [英] c# wpf polygon to bitmap not displaying anything

查看:178
本文介绍了C#WPF多边形为位图不显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果你认为这个问题已经回答了,我也到处找试图找出当我这样做,它不会显示任何内容怎么来的。
这是我所有的code:

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尝试多边形转换为位图,但它似乎并没有被显示任何内容,我可以看到。我还添加了一个画布,并添加了多边形对象那里,似乎工作。转换为位图时,它是正义的。我真的是在丢,什么是错误的,我code。我现在所拥有的一切在主窗口加载事件。

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.

帮助将AP preciated,谢谢。

Help would be appreciated, thanks.

解决方案可能是简单或东西我忽略了,希望解决办法很简单:)

The solution may be simple or something I overlooked, hopefully the solution is simple :).

推荐答案

一个WPF的UIElement有是可见之前进行布局。它必须得到至少有一个测量排列通话,它得到一个可用的大小和最后的安排矩形(通常从其父面板)。当渲染一个新创建的元素放入一个RenderTargetBitmap,你会手动调用这些方法,为大小和矩形适当的值:

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多边形为位图不显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆