PrintScreen Source Code/Simulation

∥☆過路亽.° 提交于 2019-12-11 18:17:01

问题


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.


回答1:


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;
}


来源:https://stackoverflow.com/questions/7259293/printscreen-source-code-simulation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!