PrintScreen 源代码/模拟

发布于 2024-12-02 19:49:57 字数 776 浏览 7 评论 0原文

我想拍摄一张全屏 direct3D 游戏的照片。我知道我需要创建一个“覆盖”,这在 c# 中更难,并且我发现使用 printscreen (而不是将其粘贴到 mspaint 中)确实捕获了游戏窗口。

我最终得到了这个不稳定的代码:

            try
            {
                SendKeys.SendWait("{PRTSC}");
                Thread.Sleep(100);
                if (Clipboard.ContainsImage())
                    return Clipboard.GetImage();
            }
            catch (Exception)
            {
                throw;
            }
            Bitmap bitmap = new Bitmap(1, 1);
            return bitmap;

这个代码有时可以工作,有时会抛出一个ExternalException,有时Clipboard.ContainsImage()返回false,它只是返回一个1,1大小的图像。

我想尝试改进该代码,这样我就不必依赖将某些内容复制到剪贴板所需的时间(使用 thread.sleep(20000),它会工作,但此代码只是每 800 毫秒执行一次的较大代码)。

因此,我需要有关如何更可靠地发送密钥或获取 printscreen 使用的方法的想法。

I would like to take a picture of a fullscreen direct3D game. I know I need to create an "overlay" which is harder in c# and I found out that using printscreen (and than pasting it in mspaint) does capture the game window.

I ended up with this unstable code :

            try
            {
                SendKeys.SendWait("{PRTSC}");
                Thread.Sleep(100);
                if (Clipboard.ContainsImage())
                    return Clipboard.GetImage();
            }
            catch (Exception)
            {
                throw;
            }
            Bitmap bitmap = new Bitmap(1, 1);
            return bitmap;

This code sometimes work, sometimes it throws an ExternalException and sometimes and Clipboard.ContainsImage() returns false and it simply returns a 1,1 sized image.

I would like to try and improve that code so I won't have to rely on the time it takes to copy something to the clipboard (with thread.sleep(20000), it'll work but this code is just a part of a larger code that executes every 800ms).

So I need ideas on how to send keys more reliably or get the that method printscreen uses.

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

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

发布评论

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

评论(1

生生不灭 2024-12-09 19:49:57
public static class Win32
{
    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
}

public Bitmap CaptureWindow()
{
    Bitmap bmp = new Bitmap(this.Width,this.Height);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        Win32.PrintWindow(this.Handle, g.GetHdc(), 0);
        g.ReleaseHdc();
    }
    return bmp;
}
public static class Win32
{
    [DllImport("User32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool PrintWindow(IntPtr hwnd, IntPtr hDC, uint nFlags);
}

public Bitmap CaptureWindow()
{
    Bitmap bmp = new Bitmap(this.Width,this.Height);
    using (Graphics g = Graphics.FromImage(bmp))
    {
        Win32.PrintWindow(this.Handle, g.GetHdc(), 0);
        g.ReleaseHdc();
    }
    return bmp;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文