Advertisement
Guest User

Untitled

a guest
Jan 11th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Runtime.InteropServices;
  7. using System.Drawing;
  8.  
  9. namespace ConsoleApp11
  10. {
  11. public class ScreenCapture
  12. {
  13. [DllImport("user32.dll")]
  14. private static extern IntPtr GetForegroundWindow();
  15.  
  16. [DllImport("user32.dll")]
  17. private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
  18.  
  19. [StructLayout(LayoutKind.Sequential)]
  20. private struct Rect
  21. {
  22. public int Left;
  23. public int Top;
  24. public int Right;
  25. public int Bottom;
  26. }
  27.  
  28. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  29. public static extern IntPtr GetDesktopWindow();
  30.  
  31.  
  32. public static Image CaptureDesktop()
  33. {
  34. return CaptureWindow(GetDesktopWindow());
  35. }
  36.  
  37. public static Bitmap CaptureActiveWindow()
  38. {
  39. return CaptureWindow(GetForegroundWindow());
  40. }
  41.  
  42. public static Bitmap CaptureWindow(IntPtr handle)
  43. {
  44. var rect = new Rect();
  45. GetWindowRect(handle, ref rect);
  46. var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
  47. var result = new Bitmap(bounds.Width, bounds.Height);
  48.  
  49. using (var graphics = Graphics.FromImage(result))
  50. {
  51. graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
  52. }
  53.  
  54. return result;
  55. }
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement