Guest User

Region Capture

a guest
Nov 24th, 2014
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.88 KB | None | 0 0
  1. Class RegionCapture
  2.     Inherits Form
  3.  
  4.     Public SaveName As String = Environ("UserProfile") & "\Desktop\Img.png"
  5.  
  6.     Sub New()
  7.         DoubleBuffered = True
  8.         Show()
  9.         Opacity = 0.4 : TopMost = True
  10.         FormBorderStyle = FormBorderStyle.None
  11.         WindowState = FormWindowState.Maximized
  12.     End Sub
  13.  
  14.     Private SX, SY, CX, CY As Single
  15.     Private Rect As Rectangle, Draw As Boolean = False
  16.     Protected Overrides Sub OnPaint(e As PaintEventArgs)
  17.         If Draw Then
  18.             Rect = New Rectangle(Math.Min(SX, CX), Math.Min(SY, CY), Math.Abs(SX - CX), Math.Abs(SY - CY))
  19.             e.Graphics.DrawRectangle(New Pen(Brushes.Black, 2) With {.DashStyle = Drawing2D.DashStyle.Dash}, Rect)
  20.             e.Graphics.DrawString(Rect.Width & " - " & Rect.Height, Font, Brushes.Blue, New Point(Rect.X + Rect.Width - 60, Rect.Y + Rect.Height + 10))
  21.         End If
  22.         MyBase.OnPaint(e)
  23.     End Sub
  24.     Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
  25.         Draw = True
  26.         SX = e.X : SY = e.Y
  27.         MyBase.OnMouseDown(e)
  28.     End Sub
  29.     Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
  30.         If Draw Then
  31.             CX = e.X : CY = e.Y
  32.             Invalidate()
  33.         End If
  34.         MyBase.OnMouseMove(e)
  35.     End Sub
  36.     Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
  37.         Draw = False : Opacity = 0
  38.         Dim Bm = New Bitmap(Rect.Width, Rect.Height) ', Imaging.PixelFormat.Format32bppArgb)
  39.         Dim G As Graphics = Graphics.FromImage(Bm)
  40.         G.CopyFromScreen(Rect.X, Rect.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
  41.         Bm.Save(SaveName, Imaging.ImageFormat.Png)
  42.         Close()
  43.         MyBase.OnMouseUp(e)
  44.     End Sub
  45.     Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
  46.         If e.KeyCode = Keys.Escape Then Me.Close()
  47.         MyBase.OnKeyDown(e)
  48.     End Sub
  49. End Class
Advertisement
Add Comment
Please, Sign In to add comment