Tarasovych

Untitled

Jan 10th, 2018
23
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public partial class SnippingTool : Form
  2.     {
  3.         private Rectangle rectangleSelect;
  4.         private Point pointStart;
  5.  
  6.         public Image Image { get; set; }
  7.  
  8.         public Rectangle RectangleSelect
  9.         {
  10.             set
  11.             {
  12.                 rectangleSelect = new Rectangle();
  13.             }
  14.         }
  15.  
  16.         public static Image Snip()
  17.         {
  18.             var rectangle = Screen.PrimaryScreen.Bounds;
  19.             using (Bitmap bitmap = new Bitmap(rectangle.Width, rectangle.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb))
  20.             {
  21.                 using (Graphics graphics = Graphics.FromImage(bitmap))
  22.                     graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size);
  23.  
  24.                 using (var snipper = new SnippingTool(bitmap))
  25.                 {
  26.                     if (snipper.ShowDialog() == DialogResult.OK)
  27.                     {
  28.                         return snipper.Image;
  29.                     }
  30.                 }
  31.                 return null;
  32.             }
  33.         }
  34.  
  35.         public SnippingTool(Bitmap screenShot)
  36.         {
  37.             InitializeComponent();
  38.             this.BackgroundImage = screenShot;
  39.             this.ShowInTaskbar = false;
  40.             this.FormBorderStyle = FormBorderStyle.None;
  41.             this.WindowState = FormWindowState.Maximized;
  42.             this.DoubleBuffered = true;
  43.         }
  44.  
  45.         protected override void OnMouseDown(MouseEventArgs e)
  46.         {
  47.             if (e.Button != MouseButtons.Left) return;
  48.             pointStart = e.Location;
  49.             rectangleSelect = new Rectangle(e.Location, new Size(0, 0));
  50.             this.Invalidate();
  51.         }
  52.  
  53.         protected override void OnMouseMove(MouseEventArgs e)
  54.         {
  55.             if (e.Button != MouseButtons.Left) return;
  56.             int x1 = Math.Min(e.X, pointStart.X);
  57.             int y1 = Math.Min(e.Y, pointStart.Y);
  58.             int x2 = Math.Max(e.X, pointStart.X);
  59.             int y2 = Math.Max(e.Y, pointStart.Y);
  60.             rectangleSelect = new Rectangle(x1, y1, x2 - x1, y2 - y1);
  61.             this.Invalidate();
  62.         }
  63.  
  64.         protected override void OnMouseUp(MouseEventArgs e)
  65.         {
  66.             if (rectangleSelect.Width <= 0 || rectangleSelect.Height <= 0) return;
  67.             Image = new Bitmap(rectangleSelect.Width, rectangleSelect.Height);
  68.             using (Graphics graphics = Graphics.FromImage(Image))
  69.             {
  70.                 graphics.DrawImage(this.BackgroundImage, new Rectangle(0, 0, Image.Width, Image.Height),
  71.                     rectangleSelect, GraphicsUnit.Pixel);
  72.             }
  73.             DialogResult = DialogResult.OK;
  74.         }
  75.  
  76.         protected override void OnPaint(PaintEventArgs e)
  77.         {
  78.             using (Brush brush = new SolidBrush(Color.FromArgb(120, Color.White)))
  79.             {
  80.                 int x1 = rectangleSelect.X; int x2 = rectangleSelect.X + rectangleSelect.Width;
  81.                 int y1 = rectangleSelect.Y; int y2 = rectangleSelect.Y + rectangleSelect.Height;
  82.                 e.Graphics.FillRectangle(brush, new Rectangle(0, 0, x1, this.Height));
  83.                 e.Graphics.FillRectangle(brush, new Rectangle(x2, 0, this.Width - x2, this.Height));
  84.                 e.Graphics.FillRectangle(brush, new Rectangle(x1, 0, x2 - x1, y1));
  85.                 e.Graphics.FillRectangle(brush, new Rectangle(x1, y2, x2 - x1, this.Height - y2));
  86.             }
  87.             using (Pen pen = new Pen(Color.Red, 1))
  88.             {
  89.                 e.Graphics.DrawRectangle(pen, rectangleSelect);
  90.             }
  91.         }
  92.  
  93.         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
  94.         {
  95.             if (keyData == Keys.Escape) this.DialogResult = DialogResult.Cancel;
  96.             return base.ProcessCmdKey(ref msg, keyData);
  97.         }
  98.     }
RAW Paste Data