How to capture screenshot of Window in C#

To capture a screenshot of the current active Window in C#, you can use the CopyFromScreen method of the Graphics class to copy the image of the active Window to a Bitmap object.

Here's an example code:

using System.Drawing;

// Capture the active Window screenshot
public Bitmap CaptureActiveWindow()
{
    // Get the handle of the active Window
    IntPtr hWnd = GetForegroundWindow();

    // Get the size of the Window
    Rectangle rect;
    GetWindowRect(hWnd, out rect);

    // Create a bitmap to store the screenshot
    Bitmap bmp = new Bitmap(rect.Width, rect.Height);

    // Create a graphics object from the bitmap
    using (Graphics g = Graphics.FromImage(bmp))
    {
        // Copy the image of the active Window to the bitmap
        g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size);
    }

    return bmp;
}

Note that this code uses the GetForegroundWindow and GetWindowRect Win32 functions to get the handle and size of the active Window. You'll need to include the following Win32 function signatures at the top of your code file to use them:

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

[DllImport("user32.dll")]
public static extern bool GetWindowRect(IntPtr hWnd, out Rectangle lpRect);

Also, keep in mind that capturing a screenshot of another application's Window may violate privacy and security policies.

  1. Capture screenshot of window in C#:

    • Basic method to capture a screenshot of the entire screen in C#.
    • Code snippet:
      using (Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
      {
          using (Graphics g = Graphics.FromImage(bitmap))
          {
              g.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
              bitmap.Save("screenshot.png", ImageFormat.Png);
          }
      }
      
  2. C# capture specific window screenshot:

    • Capturing a screenshot of a specific window identified by its handle in C#.
    • Code snippet:
      IntPtr windowHandle = GetWindowHandle(); // Replace with actual window handle
      CaptureWindowScreenshot(windowHandle, "specific_window_screenshot.png");
      
  3. Taking a screenshot of a specific window in C#:

    • Method to take a screenshot of a specific window using its title or handle in C#.
    • Code snippet:
      string windowTitle = "My Window";
      CaptureSpecificWindowScreenshot(windowTitle, "specific_window_screenshot.png");
      
  4. Window screenshot using C#:

    • General method to capture a screenshot of a window in C#.
    • Code snippet:
      CaptureWindowScreenshot("Window Title", "window_screenshot.png");
      
  5. C# screenshot of active window:

    • Capturing a screenshot of the currently active window in C#.
    • Code snippet:
      CaptureActiveWindowScreenshot("active_window_screenshot.png");
      
  6. Capture screenshot of WPF window in C#:

    • Taking a screenshot of a WPF window in C#.
    • Code snippet:
      Window wpfWindow = GetWpfWindow(); // Replace with actual WPF window reference
      CaptureWpfWindowScreenshot(wpfWindow, "wpf_window_screenshot.png");
      
  7. C# screenshot of Windows Form:

    • Capturing a screenshot of a Windows Form in C#.
    • Code snippet:
      Form windowsForm = GetWindowsForm(); // Replace with actual Windows Form reference
      CaptureWindowsFormScreenshot(windowsForm, "windows_form_screenshot.png");
      
  8. C# screenshot of specific process window:

    • Capturing a screenshot of a window associated with a specific process in C#.
    • Code snippet:
      Process process = GetProcess(); // Replace with actual process reference
      CaptureProcessWindowScreenshot(process, "process_window_screenshot.png");
      
  9. Capture screenshot of external application window in C#:

    • Taking a screenshot of a window from an external application in C#.
    • Code snippet:
      string externalAppName = "ExternalApp";
      CaptureExternalAppWindowScreenshot(externalAppName, "external_app_window_screenshot.png");
      
  10. Automated window screenshot in C#:

    • Automating the process of capturing screenshots of windows at regular intervals in C#.
    • Code snippet:
      Timer timer = new Timer(CaptureScreenshot, null, 0, 5000); // Capture every 5 seconds
      
  11. Using GDI to capture window screenshot in C#:

    • Utilizing the Graphics Device Interface (GDI) for capturing window screenshots in C#.
    • Code snippet:
      IntPtr windowHandle = GetWindowHandle(); // Replace with actual window handle
      CaptureWindowWithGdi(windowHandle, "gdi_window_screenshot.png");
      
  12. Capture screenshot of minimized window in C#:

    • Capturing a screenshot of a window even when it is minimized in C#.
    • Code snippet:
      IntPtr windowHandle = GetWindowHandle(); // Replace with actual window handle
      CaptureMinimizedWindowScreenshot(windowHandle, "minimized_window_screenshot.png");
      
  13. C# screenshot of console application window:

    • Taking a screenshot of a console application window in C#.
    • Code snippet:
      CaptureConsoleWindowScreenshot("console_window_screenshot.png");
      
  14. Capture screenshot of UWP window in C#:

    • Capturing a screenshot of a Universal Windows Platform (UWP) window in C#.
    • Code snippet:
      string uwpAppName = "UwpApp";
      CaptureUwpWindowScreenshot(uwpAppName, "uwp_window_screenshot.png");
      
  15. C# screenshot of multiple windows:

    • Capturing screenshots of multiple windows concurrently in C#.
    • Code snippet:
      List<IntPtr> windowHandles = GetWindowHandles(); // Replace with actual window handles
      CaptureMultipleWindowScreenshots(windowHandles, "multiple_window_screenshots");
      
  16. Taking screenshots with transparency in C#:

    • Capturing screenshots of windows with transparency in C#.
    • Code snippet:
      IntPtr windowHandle = GetWindowHandle(); // Replace with actual window handle
      CaptureTransparentWindowScreenshot(windowHandle, "transparent_window_screenshot.png");