Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Windows.Forms;
- using GlobalHotkeys;
- namespace ScreenSnap
- {
- public partial class Main : Form
- {
- #region SUBCLASS
- #region API
- /// <summary>
- /// Helper class containing User32 API functions
- /// </summary>
- private class User32
- {
- public enum WindowState
- {
- SW_SHOWNORMAL = 1,
- SW_SHOWMINIMIZED = 2,
- SW_SHOWMAXIMIZED = 3
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct WINDOWPLACEMENT
- {
- public int length;
- public int flags;
- public int showCmd;
- public System.Drawing.Point ptMinPosition;
- public System.Drawing.Point ptMaxPosition;
- public System.Drawing.Rectangle rcNormalPosition;
- }
- [StructLayout(LayoutKind.Sequential)]
- public struct RECT
- {
- public int left;
- public int top;
- public int right;
- public int bottom;
- }
- [DllImport("user32.dll")]
- public static extern int GetForegroundWindow();
- [DllImport("user32.dll")]
- public static extern IntPtr GetDesktopWindow();
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindowDC(IntPtr hWnd);
- [DllImport("user32.dll")]
- public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
- [DllImport("user32.dll")]
- public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
- [DllImport("user32.dll")]
- public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
- }
- #endregion
- #endregion
- #region METHODS
- #region CONSTRUCTORS
- public Main()
- {
- InitializeComponent();
- GlobalHotkey.RegisterHotKey(this, Keys.PrintScreen, 0);
- Keys k = Keys.PrintScreen | Keys.Alt;
- GlobalHotkey.RegisterHotKey(this, k, 1);
- TextBoxFolder.Text = Application.StartupPath;
- }
- #endregion
- /// <summary>
- /// Take screenshot
- /// </summary>
- /// <param name="Width"></param>
- /// <param name="Height"></param>
- /// <param name="X"></param>
- /// <param name="Y"></param>
- /// <returns>Image captured</returns>
- public Image ImageCapture(int Width, int Height, int X, int Y)
- {
- this.Opacity = 0;
- Rectangle ScreenBounds = new Rectangle(X, Y, Width, Height);
- Bitmap Screenshot = new Bitmap(ScreenBounds.Width, ScreenBounds.Height, PixelFormat.Format32bppArgb);
- Graphics ScreenGraph = Graphics.FromImage(Screenshot);
- ScreenGraph.CopyFromScreen(ScreenBounds.X, ScreenBounds.Y, 0, 0, ScreenBounds.Size, CopyPixelOperation.SourceCopy);
- Image Img = (Image)Screenshot;
- this.Opacity = 100;
- Clipboard.SetImage(Img);
- return Img;
- }
- /// <summary>
- /// Save image to file
- /// </summary>
- /// <param name="Img"></param>
- public void ImageSave(Image Img)
- {
- if (Directory.Exists(TextBoxFolder.Text))
- {
- string Name = TextBoxFolder.Text + @"\" + TextBoxFileName.Text + (DateTime.Now.ToFileTime()) + ".jpg";
- Img.Save(Name, ImageFormat.Jpeg);
- }
- else
- MessageBox.Show("Invalid folder", "Error");
- }
- /// <summary>
- /// Capture full screen image
- /// </summary>
- public void ScreenshotFull()
- {
- ImageSave(ImageCapture(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,
- Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y));
- }
- /// <summary>
- /// Capture window image
- /// </summary>
- public void ScreenshotWindow()
- {
- int hWnd = User32.GetForegroundWindow();
- User32.RECT bounds = new User32.RECT();
- User32.GetWindowRect((IntPtr)hWnd, ref bounds);
- int width = bounds.right - bounds.left;
- int height = bounds.bottom - bounds.top;
- User32.WINDOWPLACEMENT wp = new User32.WINDOWPLACEMENT();
- User32.GetWindowPlacement((IntPtr)hWnd, ref wp);
- int x;
- int y;
- if (wp.showCmd == (int)User32.WindowState.SW_SHOWMAXIMIZED)
- {
- x = wp.ptMaxPosition.X;
- y = wp.ptMaxPosition.Y;
- }
- else
- {
- x = wp.rcNormalPosition.X;
- y = wp.rcNormalPosition.Y;
- }
- if ((x + width) > Screen.PrimaryScreen.WorkingArea.Width)
- width = width - ((x + width) - Screen.PrimaryScreen.WorkingArea.Width);
- if ((y + height) > Screen.PrimaryScreen.WorkingArea.Height)
- height = height - ((y + height) - Screen.PrimaryScreen.WorkingArea.Height);
- ImageSave(ImageCapture(width, height, x, y));
- }
- #endregion
- #region EVENTS
- #region GLOBAL KEYS
- protected override void WndProc(ref Message m)
- {
- base.WndProc(ref m);
- if (m.Msg == GlobalHotkey.WM_HOTKEY)
- {
- switch (m.WParam.ToInt32())
- {
- case 0:
- try
- {
- ScreenshotFull();
- }
- catch
- {
- MessageBox.Show("Unknow error!", "Error");
- }
- break;
- case 1:
- try
- {
- ScreenshotWindow();
- }
- catch
- {
- MessageBox.Show("Unknow error!", "Error");
- }
- break;
- }
- }
- }
- #endregion
- private void ButtonScreenshot_Click(object sender, EventArgs e)
- {
- try
- {
- ScreenshotFull();
- }
- catch
- {
- MessageBox.Show("Unknow error!", "Error");
- }
- }
- private void ButtonFolderBrowse_Click(object sender, EventArgs e)
- {
- if (FolderBrowserDialogSave.ShowDialog() == DialogResult.OK)
- TextBoxFolder.Text = FolderBrowserDialogSave.SelectedPath;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement