想象一下 MDI 应用程序中的文档窗口,其中包含一个 WPF 子窗口,例如侧边栏。如何获得同时包含 WPF 像素和 GDI(非 wpf)像素的位图?
我发现,当对 Win7 任务栏应用程序图标悬停进行缩略图预览时,预览中 WPF 像素应在的部分会变黑。我当前的方法只是获取文档窗口的位图捕获。然后我得到一个用于预览的 DC,从中创建一个内存 DC 并在其中选择我的位图。然后我进行一些大小调整并将内存 dc 位传输到真实的 dc。我猜测 BitBlt 操作没有考虑到 WPF 像素是硬件加速的这一事实,因此需要从图形硬件中获取。 GDI 中的所有内容都管理得很好,但是当没有 WPF 子窗口时,预览图像看起来不错。
我想知道是否有可能从本机 C++ 获取 WPF 窗口的位图。然后我可以将其 blt 到上一个预览的黑色区域上。
Imagine a document window in a MDI application which contains a child WPF window, say a sidebar for example. How can one get a bitmap containing both the WPF pixels AND the GDI (non-wpf) pixels?
I've discovered that when making my thumbnail preview for the Win7 taskbar app icon hover, I get black in the parts of the preview where the WPF pixels should be. My current method simply grabs a bitmap capture of the document window. Then I get a DC for the preview, make a memory DC from it and select my bitmap into it. Then I do some size adjustments and bitblt the memory dc to the real dc. I'm guessing that the BitBlt operation doesn't take into account the fact that the WPF pixels are hardware accelerated and therefore need to be grabbed from the graphics hardware. All the stuff in GDI is managed just fine, though and when there's no WPF child windows, the preview image looks fine.
I'm wondering if it's at all possible to grab a bitmap of the WPF window from native C++. Then I can blt that onto the black area of the previous preview.
发布评论
评论(2)
也许我没有正确理解您当前的方法,但是您可以从屏幕 DC 到内存 DC 执行
BitBlt()
吗?您需要让屏幕与窗口保持一致,但这应该不会太糟糕。Maybe I'm not understanding your current approach correctly, but could you do a
BitBlt()
from the screen DC to your memory DC? You'd need to get the screen rect of your window, but that shouldn't be too bad.为了解决这个问题,我必须在本机代码中创建一个抽象类,其中包含一个虚拟方法来获取在 C++/CLI 中实现的位图。在托管实现中,我使用 .NET 的 RenderTargetBitmap 类来获取 WPF 窗口的位图捕获,然后填充传入的 CBitmap 对象(请参阅 如何在 C++/CLI 中从 RenderTargetBitmap 获取 BITMAP 结构?) 。在非托管调用程序中,我使用虚拟方法来获取位图。
简而言之,无法通过简单地使用非托管 C++ 来获取位图,因为 WPF 和 GDI 确实无法在所有实际用途上一起工作。
To solve this, I had to create an abstract class in native code containing a virtual method to get the bitmap that was implemented in C++/CLI. In the managed implementation, I used .NET's
RenderTargetBitmap
class to get a bitmap capture of the WPF window and then I filled up the passed in CBitmap object (see How to get an BITMAP struct from a RenderTargetBitmap in C++/CLI?). In the unmanaged caller routine, I used the virtual method to obtain the Bitmap.In short, there was no way to get the bitmap by simply using unmanaged C++ since WPF and GDI really don't work together for all practical purposes.