如何使一个WPF用户控件位图,而无需创建一个窗口 [英] How to render a WPF UserControl to a bitmap without creating a window

查看:144
本文介绍了如何使一个WPF用户控件位图,而无需创建一个窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能使一个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;
    }

这篇关于如何使一个WPF用户控件位图,而无需创建一个窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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