I am making a screen capturing application and everything is going fine. All I need to do is capture the active window and take a screenshot of this active window. Does an
KvanTTT's code worked great. I extended it a bit to allow a little more flexibility on save format, as well as the ability to save by hWnd, .NET Control/Form. You can get a bitmap or save to file, with a few options.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace MosaiqPerformanceMonitor {
public enum CaptureMode {
Screen, Window
}
public static class ScreenCapturer {
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
[StructLayout(LayoutKind.Sequential)]
private struct Rect {
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
/// Capture Active Window, Desktop, Window or Control by hWnd or .NET Contro/Form and save it to a specified file.
/// Filename.
/// * If extension is omitted, it's calculated from the type of file
/// * If path is omitted, defaults to %TEMP%
/// * Use %NOW% to put a timestamp in the filename
/// Optional. The default value is CaptureMode.Window.
/// Optional file save mode. Default is PNG
public static void CaptureAndSave(string filename, CaptureMode mode = CaptureMode.Window, ImageFormat format = null) {
ImageSave(filename, format, Capture(mode));
}
/// Capture a specific window (or control) and save it to a specified file.
/// Filename.
/// * If extension is omitted, it's calculated from the type of file
/// * If path is omitted, defaults to %TEMP%
/// * Use %NOW% to put a timestamp in the filename
/// hWnd (handle) of the window to capture
/// Optional file save mode. Default is PNG
public static void CaptureAndSave(string filename, IntPtr handle, ImageFormat format = null) {
ImageSave(filename, format, Capture(handle));
}
/// Capture a specific window (or control) and save it to a specified file.
/// Filename.
/// * If extension is omitted, it's calculated from the type of file
/// * If path is omitted, defaults to %TEMP%
/// * Use %NOW% to put a timestamp in the filename
/// Object to capture
/// Optional file save mode. Default is PNG
public static void CaptureAndSave(string filename, Control c, ImageFormat format = null) {
ImageSave(filename, format, Capture(c));
}
/// Capture the active window (default) or the desktop and return it as a bitmap
/// Optional. The default value is CaptureMode.Window.
public static Bitmap Capture(CaptureMode mode = CaptureMode.Window) {
return Capture(mode == CaptureMode.Screen ? GetDesktopWindow() : GetForegroundWindow());
}
/// Capture a .NET Control, Form, UserControl, etc.
/// Object to capture
/// Bitmap of control's area
public static Bitmap Capture(Control c) {
return Capture(c.Handle);
}
/// Capture a specific window and return it as a bitmap
/// hWnd (handle) of the window to capture
public static Bitmap Capture(IntPtr handle) {
Rectangle bounds;
var rect = new Rect();
GetWindowRect(handle, ref rect);
bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
CursorPosition = new Point(Cursor.Position.X - rect.Left, Cursor.Position.Y - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);
using (var g = Graphics.FromImage(result))
g.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
return result;
}
/// Position of the cursor relative to the start of the capture
public static Point CursorPosition;
/// Save an image to a specific file
/// Filename.
/// * If extension is omitted, it's calculated from the type of file
/// * If path is omitted, defaults to %TEMP%
/// * Use %NOW% to put a timestamp in the filename
/// Optional file save mode. Default is PNG
/// Image to save. Usually a BitMap, but can be any
/// Image.
static void ImageSave(string filename, ImageFormat format, Image image) {
format = format ?? ImageFormat.Png;
if (!filename.Contains("."))
filename = filename.Trim() + "." + format.ToString().ToLower();
if (!filename.Contains(@"\"))
filename = Path.Combine(Environment.GetEnvironmentVariable("TEMP") ?? @"C:\Temp", filename);
filename = filename.Replace("%NOW%", DateTime.Now.ToString("yyyy-MM-dd@hh.mm.ss"));
image.Save(filename, format);
}
}
}