在全屏3D应用程序中获取桌面的屏幕截图

使用全屏3D应用程序(如游戏)时,是否可以将桌面渲染成屏幕截图? 或者在游戏运行时,Windowsclosures渲染引擎?

我正在寻找方法将桌面渲染成我的游戏中的纹理。 RDP可以像协议一样解决吗?

编辑:为了澄清,是否有任何深层次的API机制强制渲染到另一个缓冲区,例如当屏幕截图。 没关系,如果只是Windows 7或Windows 8/9。

您可以通过在桌面窗口的hWnd中调用PrintWindow Win32 API函数来获得屏幕截图。 我在Windows 7和Windows 8.1上试过,即使在另一个应用程序全屏运行(VLC Player)的情况下也能正常工作,所以游戏也有机会。 这只会让你的桌面没有任务栏的形象,但没有其他可见的正在运行的应用程序将显示在其上。 如果你需要这些,那么你也需要获得他们的HWND(例如枚举所有正在运行的进程并获得窗口句柄),然后使用相同的方法。

using System; using System.Drawing; // add reference to the System.Drawing.dll using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; namespace DesktopScreenshot { class Program { static void Main(string[] args) { // get the desktop window handle without the task bar // if you only use GetDesktopWindow() as the handle then you get and empty image IntPtr desktopHwnd = FindWindowEx(GetDesktopWindow(), IntPtr.Zero, "Progman", "Program Manager"); // get the desktop dimensions // if you don't get the correct values then set it manually var rect = new Rectangle(); GetWindowRect(desktopHwnd, ref rect); // saving the screenshot to a bitmap var bmp = new Bitmap(rect.Width, rect.Height); Graphics memoryGraphics = Graphics.FromImage(bmp); IntPtr dc = memoryGraphics.GetHdc(); PrintWindow(desktopHwnd, dc, 0); memoryGraphics.ReleaseHdc(dc); // and now you can use it as you like // let's just save it to the desktop folder as a png file string desktopDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); string screenshotPath = Path.Combine(desktopDir, "desktop.png"); bmp.Save(screenshotPath, ImageFormat.Png); } [DllImport("User32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool PrintWindow(IntPtr hwnd, IntPtr hdc, uint nFlags); [DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr handle, ref Rectangle rect); [DllImport("user32.dll", EntryPoint = "GetDesktopWindow")] static extern IntPtr GetDesktopWindow(); [DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle); } }