当使用全屏3D应用程序(如游戏)时,是否可以将桌面渲染成屏幕快照?还是在游戏运行时窗口会关闭渲染引擎?
我正在寻找在我的游戏中将桌面渲染成纹理的方法。像RDP这样的协议能成为解决方案吗?
编辑:为了澄清,是否有任何深层次的api机制来强制呈现到另一个缓冲区,例如当屏幕截图时。不管是windows 7还是Windows 8/9。
发布于 2014-09-06 01:37:22
您可以通过在桌面窗口的PrintWindow Win32 API函数上调用hWnd来获得屏幕快照。我在Windows 7和Windows8.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);
}
}
https://stackoverflow.com/questions/25500137
复制相似问题