如何使一个WPF用户控件位图,而无需创建一个窗口位图、控件、创建一个、窗口

2023-09-02 10:50:03 作者:▍限量版男人

我怎样才能使一个WPF用户控件位图,而无需创建一个窗口?我需要渲染一个WPF用户控件,并把它上传到另一个程序。该位图将通过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;
    }