locked
How do I capture a screenshot of a process’s main window? RRS feed

  • Question

  • How do I capture a screenshot of a process’s main window?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 7, 2009 2:01 PM

Answers

  • Here is one method to take a process screenshot.  We will use the process “notepad” for this example.   Please follow these steps:

    1.   Use System.Diagnostics.Process.MainWindowHandle to get the process information we require to take a screenshot.

    2.   Use the Windows API, SetForegroundWindow, to activate the target process.

    3.   Pass the window handle to the first parameter of the Windows API GetWindowRect, to get the location information of the target process.

    4.   Define the coordinates of the process window on the screen with the location information in step 3.

    5.   Use the coordinates in step 4 and Graphics.CopyFromScreen to take the screenshot.

     

     public partial class Form1 : Form
        {
            [DllImport("user32.dll")]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);
            [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
            [return: MarshalAs(UnmanagedType.Bool)]
            private static extern bool SetForegroundWindow(IntPtr hWnd);
            [StructLayout(LayoutKind.Sequential)]
            public struct RECT
            {
                public int Left;
                public int Top;
                public int Right;
                public int Bottom;
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Process proc = new Process();
                proc.StartInfo.FileName = "notepad.exe";
                proc.Start()
                proc.WaitFoInputIdle();
                if (SetForegroundWindow(proc.MainWindowHandle))
                {
                    RECT srcRect;
                    if (!proc.MainWindowHandle.Equals(IntPtr.Zero))
                    {
                        if (GetWindowRect(proc.MainWindowHandle, out srcRect))
                        {
                            int width = srcRect.Right - srcRect.Left;
                            int height = srcRect.Bottom - srcRect.Top;
    
                            Bitmap bmp = new Bitmap(width, height);
                            Graphics screenG = Graphics.FromImage(bmp);
    
                            try
                            {
                                screenG.CopyFromScreen(srcRect.Left, srcRect.Top,   
                                        0, 0, new Size(width, height),
                                        CopyPixelOperation.SourceCopy);
    
                                bmp.Save("notepad.jpg", ImageFormat.Jpeg);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                            finally
                            {
                                screenG.Dispose();
                                bmp.Dispose();
                            }                     
                         }
                     } 
                }
            }
        }
    
    

     

    Related thread:
    http://social.msdn.microsoft.com/forums/en-US/Vsexpressvcs/thread/3571babb-e20c-4c38-94ff-a49bc0c92e56/

     

    For more FAQ about Visual C# General, please see Visual C# General FAQ

     



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Tuesday, April 7, 2009 2:01 PM