如何在不创建窗口的情况下将 WPF UserControl 渲染为位图 [英] How to render a WPF UserControl to a bitmap without creating a window
问题描述
如何在不创建窗口的情况下将 WPF UserControl 渲染为位图?我需要呈现 WPF UserControl 并将其上传到另一个程序.位图将通过 Windows 服务呈现,因此创建窗口不是一个选项(我知道有虚拟"创建窗口的方法,但不幸的是,在我的情况下,调用命令来创建窗口的任何内容都不是一个选项).有没有办法渲染用户控件而不将其绑定到窗口?
How can I render a WPF UserControl to a bitmap without creating a window? I need to render a WPF UserControl and upload it to another program. The bitmaps will be rendered through a Windows Service, so creating a window is not an option (I know there's ways to 'virtually' create windows, but unfortunately anything that calls a command to create a window is NOT an option in my case). Is there a way to RENDER the UserControl without binding it to a Window?
推荐答案
最终使用了没有实际窗口的 HwndHost.
Ended up using an HwndHost with no actual window.
void cwind()
{
Application myapp = new Application();
mrenderer = new WPFRenderer();
mrenderer.Width = 256;
mrenderer.Height = 256;
HwndSourceParameters myparms = new HwndSourceParameters();
HwndSource msrc = new HwndSource(myparms);
myparms.HwndSourceHook = new HwndSourceHook(ApplicationMessageFilter);
msrc.RootVisual = mrenderer;
myapp.Run();
}
static IntPtr ApplicationMessageFilter(
IntPtr hwnd, int message, IntPtr wParam, IntPtr lParam, ref bool handled)
{
return IntPtr.Zero;
}
这篇关于如何在不创建窗口的情况下将 WPF UserControl 渲染为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!