Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Runtime.InteropServices;
- using System.Drawing;
- namespace ConsoleApp11
- {
- public class ScreenCapture
- {
- [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();
- public static Image CaptureDesktop()
- {
- return CaptureWindow(GetDesktopWindow());
- }
- public static Bitmap CaptureActiveWindow()
- {
- return CaptureWindow(GetForegroundWindow());
- }
- public static Bitmap CaptureWindow(IntPtr handle)
- {
- var rect = new Rect();
- GetWindowRect(handle, ref rect);
- var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
- var result = new Bitmap(bounds.Width, bounds.Height);
- using (var graphics = Graphics.FromImage(result))
- {
- graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
- }
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement