C#Bitblt截图不适用于所有系统

时间:2018-05-07 16:22:06

标签: c# gdi+ directx-11 bitblt

我正在尝试截取DirectX游戏的截图。我想捕捉窗户,即使它在其他窗户后面而不将窗户带到前面。当我在我的系统上运行应用程序时,它完美地运行。但是,在我运行应用程序的所有其他系统上,它只捕获边框,内部区域为黑色。

My system, works

Second system, only border

Third system, only border

我的代码:

// get the hDC of the target window
IntPtr hdcSrc = User32.GetWindowDC(hWnd);

// get the size
Rectangle windowRect = new Rectangle();
User32.GetWindowRect(hWnd, ref windowRect);
int width = windowRect.Right - windowRect.Left;
int height = windowRect.Bottom - windowRect.Top;

// create a device context we can copy to
IntPtr hdcDest = Gdi32.CreateCompatibleDC(hdcSrc);

// create a bitmap we can copy it to,
// using GetDeviceCaps to get the width/height
IntPtr hBitmap = Gdi32.CreateCompatibleBitmap(hdcSrc, width, height);

// select the bitmap object
IntPtr hOld = Gdi32.SelectObject(hdcDest, hBitmap);

// bitblt over
Gdi32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY);

// restore selection
Gdi32.SelectObject(hdcDest, hOld);

// clean up 
Gdi32.DeleteDC(hdcDest);
User32.ReleaseDC(hWnd, hdcSrc);

// get a .NET image object for it
Image img = Image.FromHbitmap(hBitmap);

// free up the Bitmap object
Gdi32.DeleteObject(hBitmap);

if (bmp != null)
    bmp.Dispose();
bmp = new Bitmap(img);

如果重要,我的系统显卡是NVIDIA GeForce GTX 970,第二款是NVIDIA GeForce GTX 1060,第三款是NVIDIA GeForce GTX 960M。所有系统都是Windows 10 64位。

0 个答案:

没有答案