是否可以使用本机c++从WPF窗口捕获位图?

时间:2022-09-02 00:22:10

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?

想象一个MDI应用程序中的文档窗口,它包含一个子WPF窗口,例如一个侧栏。如何获得包含WPF像素和GDI(非WPF)像素的位图?

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.

我发现,在为Win7任务栏应用程序图标进行缩略图预览时,WPF像素应该位于的预览部分变为黑色。我当前的方法只是抓取文档窗口的位图捕获。然后我得到一个DC用于预览,从它做一个内存DC并选择我的位图到它里面。然后我做了一些尺寸调整和位blt的内存dc到真正的dc。我猜测BitBlt操作没有考虑到WPF像素是硬件加速的事实,因此需要从图形硬件中获取。GDI中的所有东西都可以很好地管理,但是当没有WPF子窗口时,预览图像看起来很好。

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.

我想知道是否有可能从本机c++获取WPF窗口的位图。然后我可以把它涂到先前预览的黑色区域。

2 个解决方案

#1


0  

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.

也许我没有正确地理解您当前的方法,但是您可以从屏幕DC到内存DC做一个BitBlt()吗?你需要得到窗口的屏幕矩形,但这不应该太糟糕。

#2


0  

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.

为了解决这个问题,我必须在本机代码中创建一个抽象类,其中包含一个虚拟方法,以获得在c++ /CLI中实现的位图。在托管实现中,我使用。net的RenderTargetBitmap类获取WPF窗口的位图捕获,然后填充传递的CBitmap对象(参见如何从c++ /CLI的RenderTargetBitmap获取位图结构)。在非托管调用程序中,我使用虚拟方法获取位图。

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.

简而言之,仅仅使用非托管的c++是无法获得位图的,因为WPF和GDI实际上并没有实现所有的实际目的。

#1


0  

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.

也许我没有正确地理解您当前的方法,但是您可以从屏幕DC到内存DC做一个BitBlt()吗?你需要得到窗口的屏幕矩形,但这不应该太糟糕。

#2


0  

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.

为了解决这个问题,我必须在本机代码中创建一个抽象类,其中包含一个虚拟方法,以获得在c++ /CLI中实现的位图。在托管实现中,我使用。net的RenderTargetBitmap类获取WPF窗口的位图捕获,然后填充传递的CBitmap对象(参见如何从c++ /CLI的RenderTargetBitmap获取位图结构)。在非托管调用程序中,我使用虚拟方法获取位图。

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.

简而言之,仅仅使用非托管的c++是无法获得位图的,因为WPF和GDI实际上并没有实现所有的实际目的。