Advertisement
Guest User

Knx

a guest
Apr 21st, 2009
1,677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.00 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Windows.Forms;
  7. using GlobalHotkeys;
  8.  
  9. namespace ScreenSnap
  10. {
  11.     public partial class Main : Form
  12.     {
  13.         #region SUBCLASS
  14.         #region API
  15.         /// <summary>
  16.         /// Helper class containing User32 API functions
  17.         /// </summary>
  18.         private class User32
  19.         {
  20.             public enum WindowState
  21.             {
  22.                 SW_SHOWNORMAL = 1,
  23.                 SW_SHOWMINIMIZED = 2,
  24.                 SW_SHOWMAXIMIZED = 3
  25.             }
  26.             [StructLayout(LayoutKind.Sequential)]
  27.             public struct WINDOWPLACEMENT
  28.             {
  29.                 public int length;
  30.                 public int flags;
  31.                 public int showCmd;
  32.                 public System.Drawing.Point ptMinPosition;
  33.                 public System.Drawing.Point ptMaxPosition;
  34.                 public System.Drawing.Rectangle rcNormalPosition;
  35.             }
  36.             [StructLayout(LayoutKind.Sequential)]
  37.             public struct RECT
  38.             {
  39.                 public int left;
  40.                 public int top;
  41.                 public int right;
  42.                 public int bottom;
  43.             }
  44.             [DllImport("user32.dll")]
  45.             public static extern int GetForegroundWindow();
  46.             [DllImport("user32.dll")]
  47.             public static extern IntPtr GetDesktopWindow();
  48.             [DllImport("user32.dll")]
  49.             public static extern IntPtr GetWindowDC(IntPtr hWnd);
  50.             [DllImport("user32.dll")]
  51.             public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
  52.             [DllImport("user32.dll")]
  53.             public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
  54.             [DllImport("user32.dll")]
  55.             public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
  56.         }
  57.         #endregion
  58.         #endregion
  59.  
  60.         #region METHODS
  61.         #region CONSTRUCTORS
  62.         public Main()
  63.         {
  64.             InitializeComponent();
  65.  
  66.             GlobalHotkey.RegisterHotKey(this, Keys.PrintScreen, 0);
  67.             Keys k = Keys.PrintScreen | Keys.Alt;
  68.             GlobalHotkey.RegisterHotKey(this, k, 1);
  69.  
  70.             TextBoxFolder.Text = Application.StartupPath;
  71.         }
  72.         #endregion
  73.  
  74.         /// <summary>
  75.         /// Take screenshot
  76.         /// </summary>
  77.         /// <param name="Width"></param>
  78.         /// <param name="Height"></param>
  79.         /// <param name="X"></param>
  80.         /// <param name="Y"></param>
  81.         /// <returns>Image captured</returns>
  82.         public Image ImageCapture(int Width, int Height, int X, int Y)
  83.         {
  84.             this.Opacity = 0;
  85.             Rectangle ScreenBounds = new Rectangle(X, Y, Width, Height);
  86.             Bitmap Screenshot = new Bitmap(ScreenBounds.Width, ScreenBounds.Height, PixelFormat.Format32bppArgb);
  87.             Graphics ScreenGraph = Graphics.FromImage(Screenshot);
  88.             ScreenGraph.CopyFromScreen(ScreenBounds.X, ScreenBounds.Y, 0, 0, ScreenBounds.Size, CopyPixelOperation.SourceCopy);
  89.             Image Img = (Image)Screenshot;
  90.             this.Opacity = 100;
  91.             Clipboard.SetImage(Img);
  92.             return Img;
  93.         }
  94.  
  95.         /// <summary>
  96.         /// Save image to file
  97.         /// </summary>
  98.         /// <param name="Img"></param>
  99.         public void ImageSave(Image Img)
  100.         {
  101.             if (Directory.Exists(TextBoxFolder.Text))
  102.             {
  103.                 string Name = TextBoxFolder.Text + @"\" + TextBoxFileName.Text + (DateTime.Now.ToFileTime()) + ".jpg";
  104.                 Img.Save(Name, ImageFormat.Jpeg);
  105.             }
  106.             else
  107.                 MessageBox.Show("Invalid folder", "Error");
  108.         }
  109.  
  110.         /// <summary>
  111.         /// Capture full screen image
  112.         /// </summary>
  113.         public void ScreenshotFull()
  114.         {
  115.             ImageSave(ImageCapture(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,
  116.                     Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y));
  117.         }
  118.  
  119.         /// <summary>
  120.         /// Capture window image
  121.         /// </summary>
  122.         public void ScreenshotWindow()
  123.         {
  124.             int hWnd = User32.GetForegroundWindow();
  125.             User32.RECT bounds = new User32.RECT();
  126.             User32.GetWindowRect((IntPtr)hWnd, ref bounds);
  127.             int width = bounds.right - bounds.left;
  128.             int height = bounds.bottom - bounds.top;
  129.             User32.WINDOWPLACEMENT wp = new User32.WINDOWPLACEMENT();
  130.             User32.GetWindowPlacement((IntPtr)hWnd, ref wp);
  131.             int x;
  132.             int y;
  133.  
  134.             if (wp.showCmd == (int)User32.WindowState.SW_SHOWMAXIMIZED)
  135.             {
  136.                 x = wp.ptMaxPosition.X;
  137.                 y = wp.ptMaxPosition.Y;
  138.             }
  139.             else
  140.             {
  141.                 x = wp.rcNormalPosition.X;
  142.                 y = wp.rcNormalPosition.Y;
  143.             }
  144.  
  145.             if ((x + width) > Screen.PrimaryScreen.WorkingArea.Width)
  146.                 width = width - ((x + width) - Screen.PrimaryScreen.WorkingArea.Width);
  147.  
  148.             if ((y + height) > Screen.PrimaryScreen.WorkingArea.Height)
  149.                 height = height - ((y + height) - Screen.PrimaryScreen.WorkingArea.Height);
  150.  
  151.             ImageSave(ImageCapture(width, height, x, y));
  152.         }
  153.         #endregion
  154.  
  155.         #region EVENTS
  156.         #region GLOBAL KEYS
  157.         protected override void WndProc(ref Message m)
  158.         {
  159.             base.WndProc(ref m);
  160.  
  161.             if (m.Msg == GlobalHotkey.WM_HOTKEY)
  162.             {
  163.                 switch (m.WParam.ToInt32())
  164.                 {
  165.                     case 0:
  166.                         try
  167.                         {
  168.                             ScreenshotFull();
  169.                         }
  170.                         catch
  171.                         {
  172.                             MessageBox.Show("Unknow error!", "Error");
  173.                         }
  174.                         break;
  175.                     case 1:
  176.                         try
  177.                         {
  178.                             ScreenshotWindow();
  179.                         }
  180.                         catch
  181.                         {
  182.                             MessageBox.Show("Unknow error!", "Error");
  183.                         }
  184.                         break;
  185.                 }
  186.             }
  187.         }
  188.         #endregion
  189.  
  190.         private void ButtonScreenshot_Click(object sender, EventArgs e)
  191.         {
  192.             try
  193.             {
  194.                 ScreenshotFull();
  195.             }
  196.             catch
  197.             {
  198.                 MessageBox.Show("Unknow error!", "Error");
  199.             }
  200.         }
  201.  
  202.         private void ButtonFolderBrowse_Click(object sender, EventArgs e)
  203.         {
  204.             if (FolderBrowserDialogSave.ShowDialog() == DialogResult.OK)
  205.                 TextBoxFolder.Text = FolderBrowserDialogSave.SelectedPath;
  206.         }
  207.         #endregion
  208.     }
  209. }
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement