使用 directX 捕获并绘制屏幕截图

发布于 2024-10-29 09:17:47 字数 1926 浏览 4 评论 0原文

我试图让 DirectX (DX9) 抓取桌面的屏幕截图并立即将其绘制回(以较小的尺寸)到我的表单中。

我让 DirectX 发挥作用,将设备与一些表面一起创建,然后我可以将它们渲染到屏幕上。我正在使用一个表面 F3F3Surf9_SS 来获取桌面屏幕截图。

这是我的变量声明和初始化

F3D3Surf9_SS : IDirect3DSurface9; //Surface SS
F3D3Surf9_A : IDirect3DSurface9;  //Surface A
F3D3Surf9_B : IDirect3DSurface9;  //Surface B

...

FDirect3D9.CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,Form1.Handle,
                        D3DCREATE_SOFTWARE_VERTEXPROCESSING,@D3DPresentParams,
                        FDirect3DDevice9);

FDirect3DDevice9.CreateOffscreenPlainSurface(1360,768,D3DFMT_X8R8G8B8,
                                             D3DPOOL_DEFAULT,F3D3Surf9_A,nil);

D3DXLoadSurfaceFromFile(F3D3Surf9_A,nil,nil,'D:\Images\Pillar.bmp',nil,
                        D3DX_DEFAULT,0,nil);

FDirect3DDevice9.CreateOffscreenPlainSurface(1360,768,D3DFMT_X8R8G8B8,
                                             D3DPOOL_DEFAULT,F3D3Surf9_B,nil);

D3DXLoadSurfaceFromFile(F3D3Surf9_B,nil,nil,'D:\Images\Niagra.bmp',nil,
                        D3DX_DEFAULT,0,nil);

FDirect3DDevice9.CreateOffscreenPlainSurface(Screen.Width,Screen.Height,D3DFMT_A8R8G8B8,
                                         D3DPOOL_SCRATCH,F3D3Surf9_SS,nil);

这是我用来抓取然后渲染屏幕截图的代码

FDIrect3DDevice9.BeginScene;

FDirect3DDevice9.Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),0,0);
FDirect3DDevice9.GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, BackBuffer);

FDirect3DDevice9.GetFrontBufferData(0,F3D3Surf9_SS);  //Get the screen shot

FDirect3DDevice9.StretchRect(F3D3Surf9_SS,nil,BackBuffer,nil,D3DTEXF_NONE); //Draw it

FDIrect3DDevice9.EndScene;
FDirect3DDevice9.Present(nil,nil,0,nil);

但这不起作用。

图像不会绘制到屏幕上。如果我将表面 A 或 B 绘制到屏幕上,这可以工作,但对于 Surface SS 不起作用。不过我知道 Surface SS 中有屏幕截图,因为如果我调用 D3DXSaveSurfaceToFile ,我放在硬盘上的结果位图就是有效的屏幕截图。

关于执行此操作的正确方法有什么想法吗?

I am trying to get DirectX (DX9) to grab a screenshot of the desktop and immediately draw it back out (in smaller dimensions) to my form.

I have DirectX working to the capacity that the device is created along with a few surfaces and I can render them to screen. I am using one surface F3F3Surf9_SS to get the desktop Screenshot.

Here is my declaration and initialization of varaibles

F3D3Surf9_SS : IDirect3DSurface9; //Surface SS
F3D3Surf9_A : IDirect3DSurface9;  //Surface A
F3D3Surf9_B : IDirect3DSurface9;  //Surface B

...

FDirect3D9.CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,Form1.Handle,
                        D3DCREATE_SOFTWARE_VERTEXPROCESSING,@D3DPresentParams,
                        FDirect3DDevice9);

FDirect3DDevice9.CreateOffscreenPlainSurface(1360,768,D3DFMT_X8R8G8B8,
                                             D3DPOOL_DEFAULT,F3D3Surf9_A,nil);

D3DXLoadSurfaceFromFile(F3D3Surf9_A,nil,nil,'D:\Images\Pillar.bmp',nil,
                        D3DX_DEFAULT,0,nil);

FDirect3DDevice9.CreateOffscreenPlainSurface(1360,768,D3DFMT_X8R8G8B8,
                                             D3DPOOL_DEFAULT,F3D3Surf9_B,nil);

D3DXLoadSurfaceFromFile(F3D3Surf9_B,nil,nil,'D:\Images\Niagra.bmp',nil,
                        D3DX_DEFAULT,0,nil);

FDirect3DDevice9.CreateOffscreenPlainSurface(Screen.Width,Screen.Height,D3DFMT_A8R8G8B8,
                                         D3DPOOL_SCRATCH,F3D3Surf9_SS,nil);

Here is the code I use to grab and then render the screenshot

FDIrect3DDevice9.BeginScene;

FDirect3DDevice9.Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),0,0);
FDirect3DDevice9.GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, BackBuffer);

FDirect3DDevice9.GetFrontBufferData(0,F3D3Surf9_SS);  //Get the screen shot

FDirect3DDevice9.StretchRect(F3D3Surf9_SS,nil,BackBuffer,nil,D3DTEXF_NONE); //Draw it

FDIrect3DDevice9.EndScene;
FDirect3DDevice9.Present(nil,nil,0,nil);

However this does not work.

The image does not get drawn to screen. If I draw surface A or B to screen, that works but it doesn't work for Surface SS. However I know Surface SS has the screenshot in it since if I call D3DXSaveSurfaceToFile the resulting bitmap I put on the hard disk is a valid screen shot.

Any thoughts on the proper way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-11-05 09:17:47

这不起作用的原因是 F3D3Surf9_SS 是由 D3DPOOL_SCRATCH 在系统内存中声明的,并且无法像我尝试的那样直接绘制到后台缓冲区。

所以我的解决方案是使用F3D3Surf9_A表面并使用UpdateSurface将系统内存中的屏幕截图复制到视频内存中的表面A中。

要使其正常工作,我必须做的唯一其他更改是以与屏幕截图表面相同的格式创建表面 A:D3DFMT_A8R8G8B8。还必须确保 UpdateSurface 中的目标曲面大于源曲面。

注意:

  • 这很慢,因为我们从视频内存读取到系统内存,然后再返回视频内存。
  • 我的应用程序需要这个,因为我想捕获操作系统和其他应用程序在屏幕上显示的所有内容,但如果您只是担心自己的应用程序,那么还有更好的选择。
  • 如果您知道一种无需将其放入系统内存即可获取FrontBufferData的方法(这是我可以看到它工作的唯一方法),请告诉我。

The reason this would not work is that the F3D3Surf9_SS was declared in system memory by D3DPOOL_SCRATCH and cannot be drawn directly to the back buffer as I was trying to.

So my solution was to use the F3D3Surf9_A surface and use UpdateSurface to copy the screenshot in system memory into the surface A in video memory.

The only other change I had to make to get this to work was create Surface A in the same format as the screenshot surface: D3DFMT_A8R8G8B8. Also had to make sure the the destination surface in UpdateSurface was larger than the source surface.

NOTE:

  • This is slow since we are reading from video memory to system memory and then right back to video memory.
  • I needed this for my application since I want to capture everything the OS and other application put on screen but if you are just worried about your own application then there are better alternatives.
  • If you know of a way to GetFrontBufferData without putting it to system memory (which is the only way I could see it working) please let me know.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文