快速分层WPF窗口的位图捕获 [英] Fast bitmap capture of layered WPF window

查看:82
本文介绍了快速分层WPF窗口的位图捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在寻找一种获取给定分层WPF窗口(使用窗口句柄)的位图的快速方法.该窗口是半透明的WPF窗口,应用程序在 Windows XP (不是Vista或7),.

在研究DirectX之前,我尝试了很多事情:

-GDI +:速度很快,但我无法保持真正的透明度(无法摆脱背景窗口/桌面,我想保留WPF窗口的图形,别无其他)


-.NET/WPF捕获功能(RenderTargetBitmap等):保留透明度,但速度太慢

因此,我正在寻找某种快速捕获功能(〜50ms),并且我认为DirectX可能是一种解决方案.完全没有DirectX经验,我尝试了几件事,但没有达到我预期的效果:)

我能想到的最好的方法是使用SlimDX(我也不是一个出色的C ++编码器;-))并修改一些在网上找到的代码:

使用(SlimDX.Direct3D9.Surface surface = device. GetRenderTarget (0))
           {
                            使用(SlimDX.Direct3D9.Surface surface2 = SlimDX.Direct3D9.Surface. CreateOffscreenPlain (
              nbsp; bsp               nbsp; bsp          设备表面.Description.Width,
              nbsp; bsp               nbsp; bsp          surface.Description.Height,
              nbsp; bsp               nbsp; bsp          surface.Description.Format,
              nbsp; bsp               nbsp; bsp          SlimDX.Direct3D9.Pool.SystemMemory))
                             {
                    GetRenderTargetData (surface,surface2);
                    var dataStream = SlimDX.Direct3D9.Surface.ToStream(
              nbsp; bsp              surface2,
              nbsp; bsp              SlimDX.Direct3D9.ImageFileFormat.Bmp,
              nbsp; bsp              NativeMethods.GetClientRect(句柄) //窗口客户区"
              nbsp; bsp              );
                   位图=新位图(数据流);
                             }
           }


这确实是非常快的(<50ms),但是...却行不通.当将位图保存到文件中时,我得到的只是一些不连贯的东西,我的窗口的点点滴滴是随机剪切和布置的,还有其他地方的东西.

任何帮助将不胜感激!

谢谢.

解决方案

Fabrice,

一些说明:

1.目标窗口是否属于进行捕获的同一进程? (看起来确实是因为您提到了实时出价,但只是想确定一下)

2.您是说要使捕获的图像保持透明度,而不是将窗口/桌面保留在目标窗口的后面吗?

我可以知道为什么有50ms的条形吗?

谢谢,

在论坛中 MSDN订阅者支持 如果您对我们的支持有任何反馈,请联系 msdnmg@microsoft.com


Hi,

I'm looking for a fast way to get a bitmap of a given layered WPF window (using a window handle). The window is a semi-transparent WPF window, and the application is running under Windows XP (not Vista or 7, this is a strong requirement).

I tried quite a few things before looking into DirectX :

- GDI+ : fast but I could not preserve real transparency (can't get rid of the background window/desktop, I want to keep the graphics of the WPF window, nothing else)


- .NET/WPF capturing features (RenderTargetBitmap etc.) : preserve transparency but way too slow

So, I was looking for some kind of fast capture function (~50ms) and I thought maybe DirectX could be a solution. Having no experience at all with DirectX, I tried a couple of things but nothing worked the way I expected :)

The best I could came up was using SlimDX (I'm not a great C++ coder either ;-) ) and modifying some code found online :

using (SlimDX.Direct3D9.Surface surface = device.GetRenderTarget (0))
            {
                using (SlimDX.Direct3D9.Surface surface2 = SlimDX.Direct3D9.Surface.CreateOffscreenPlain (
                                                            device, surface.Description.Width,
                                                            surface.Description.Height,
                                                            surface.Description.Format,
                                                            SlimDX.Direct3D9.Pool.SystemMemory))
                {
                    device.GetRenderTargetData (surface, surface2);
                    var dataStream = SlimDX.Direct3D9.Surface.ToStream(
                                        surface2,
                                        SlimDX.Direct3D9.ImageFileFormat.Bmp,
                                        NativeMethods.GetClientRect(handle) // Window 'client area'
                                        );
                    bitmap = new Bitmap(dataStream);
                }
            }


This is indeed quite fast (< 50ms) but... it doesn't work. All I get, when saving the bitmap to a file, is some incoherent stuff with bits and pieces of my window randomly cut and laid out plus other stuff from somewhere else.

Any help would be greatly appreciated!

Thanks.

解决方案

Hi Fabrice,

Some clarifications:

1. Does the target window belong to the same process that is doing the capture? (looks like it does because you mentioned RTB, but just want to be sure)

2. Do you mean you want the captured image to keep the transparency while it doesn't keep the window/desktop in the back of the target window?

And may I know why there is the 50ms bar?

Thanks,
Jie
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com


这篇关于快速分层WPF窗口的位图捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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