首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在全屏3D应用程序中获取桌面屏幕截图

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

Stack Overflow用户
提问于 2014-08-26 07:18:44
回答 1查看 3.5K关注 0票数 1

当使用全屏3D应用程序(如游戏)时,是否可以将桌面渲染成屏幕快照?还是在游戏运行时窗口会关闭渲染引擎?

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

编辑:为了澄清,是否有任何深层次的api机制来强制呈现到另一个缓冲区,例如当屏幕截图时。不管是windows 7还是Windows 8/9。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-06 01:37:22

您可以通过在桌面窗口的PrintWindow Win32 API函数上调用hWnd来获得屏幕快照。我在Windows 7和Windows8.1上尝试过这一点,即使在全屏运行另一个应用程序(VLC Player)时,它也能工作,所以它也有可能与游戏一起工作。这只会使您在没有任务栏的情况下获得桌面的图像,但是其他正在运行的应用程序都不会显示在上面。如果您也需要它们,那么您也需要获得它们的HWND (例如,通过枚举所有正在运行的进程并获取它们的窗口句柄),然后使用相同的方法。

代码语言:javascript
运行
复制
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); 
  }
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25500137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档