正如我今天发现的那样,将 WPF 控件渲染为位图图像并不是一项简单的任务。据我所知,现在处理父控件边距是一个问题,正如 Rick Strahl 在他的博客
http://www.west-wind.com/weblog/posts/2007/Sep/10/Rendering-a-WPF-Container-to-Bitmap
到目前为止,我能够创建窗口内可见的任何控件的位图,但我真正需要做的是创建不可见控件的位图。我只是在代码中创建它们 - 简单的形状,如矩形和椭圆形 - 并希望将它们作为位图保存到磁盘。
对我来说,这简直就是一场个人噩梦。由于我的 ActualHeight 和 ActualWidth 始终为 0,所以我使用 Height 和 Width 来代替。但我得到的只是一个与我的控件大小相同的空图像。
如何创建任何控件的位图而不将其添加到可见窗口?
Render a WPF control to a bitmap image is not a trivial task as I found out today. As I know now dealing with the parent control margin is a problem, as Rick Strahl wrote in his blog
http://www.west-wind.com/weblog/posts/2007/Sep/10/Rendering-a-WPF-Container-to-Bitmap
So far I am able to create bitmaps of any control that is visible inside a window but what I really need to do is create bitmaps of invisible controls. I just create them in code - simples shapes like rectangle and ellipse - and want to save them as bitmaps to disk.
For me this turn out to be a personal nightmare. Since my ActualHeight and ActualWidth is always 0 I use Height and Width instead. But all I get is a empty image in the size of my control.
How can I create a bitmap of any control without adding it to a visible window?
发布评论
评论(1)
新元素尚未执行布局。在渲染之前,您需要在控件上调用 Measure 和 Arrange。
New elements haven't had their layout performed. You need to call Measure and Arrange on the control before you render it.