开发者

PrintScreen Source Code/Simulation

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 an开发者_StackOverflow社区d 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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜