0

I am trying to take the screenshot of a particular area (application) in the desktop. Please, check this code...

        try
        {
            //Process p = Process.Start("notepad");
            //Process p = Process.GetProcessById(11152);
            if (p == null)
                Console.WriteLine("Got Null");
            else
            {
            IntPtr h = p.Handle;
            SetForegroundWindow(h);
            ShowWindow(h, 9);
            Rect rect = new Rect();
            IntPtr error = GetWindowRect(p.MainWindowHandle, ref rect);
            while (error == (IntPtr)0)
            {
                error = GetWindowRect(p.MainWindowHandle, ref rect);
            }
            Thread.Sleep(2000);
            int width = rect.right - rect.left;
            int height = rect.bottom - rect.top;
            System.IO.FileStream fs = System.IO.File.Create(@"D:\snapshot.jpg");
            Bitmap bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            Graphics.FromImage(bitmap).CopyFromScreen(rect.left, rect.top, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
            bitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
            fs.Close();
            bitmap.Dispose();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }

and I have this outside this method..

    [DllImport("user32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern bool IsIconic(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")]
    private static extern IntPtr ShowWindow(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);

    [StructLayout(LayoutKind.Sequential)]
    public struct Rect
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

When I try to open a new application and try to take screenshot (like notepad in this eg) it works like a charm. But when I try to take the screenshot of a currently running application (like task manager etc..) It does not work (I still get an image of small black rectangle).. Please help me..

  • Check following > http://stackoverflow.com/questions/891345/get-a-screenshot-of-a-specific-application – Sameer Jan 20 '14 at 08:48
  • I checked every post regarding this.. still could'nt find where my problem lies... please give suggestions with respect to my code.. – user3197401 Jan 20 '14 at 08:50

1 Answers1

0

Change

IntPtr h = p.Handle;

to

IntPtr h = p.MainWindowHandle;

and check.

Sameer
  • 2,143
  • 1
  • 16
  • 22
  • 1
    Yes.. p.MainWindowHandle worked but i got a minimized title bar image. So finally I used ShowWindowAsync(handle,SW_MAXIMIZED) to get the full image.But there is another problem. sometimes the application becomes active (becomes foreground) and sometimes not. Though I get the image in both the cases I still prefer that application should not becomes after the showwindowsasync function call. Is there any way of doing this ? – user3197401 Jan 20 '14 at 10:40