在 Windows 服务中,我试图将 WPF 控件呈现为 png,问题是所涉及的控件之一(即 Oxyplot PlotView)检查控件是否实际上可见,如果不可见则不绘制任何内容。我发现使 IsVisible 返回 true 的唯一方法是将控件放在窗口内并在窗口上调用
Show
,结果是弹出一个窗口,这是我不想要的。
我试图将控件放在
Page
中,但没有帮助。
这里是我使用的基本代码:
var plotView = new PlotView() { Height = height, Width = width, XPS = true, Model = plot };
var view = new ContentControl() { Content = plotView, Width= width, Height = height };
//force bindings
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.ApplicationIdle, new DispatcherOperationCallback(_ => { return null; }), null);
view.Measure(new Size(width, height));
view.Arrange(new Rect(0, 0, width, height));
view.UpdateLayout();
var encoder = new PngBitmapEncoder();
var render = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
render.Render(view);
encoder.Frames.Add(BitmapFrame.Create(render));
using (var s = File.Open(filename, FileMode.Create))
{
encoder.Save(s);
}
这是在 STA 线程中运行的。渲染其他不是
PlotView
作品的小部件,我可以在调试器中看到 PlotView 如何不绘制它的视觉效果,因为它检查它是否可见。