Guest User

Untitled

a guest
Sep 7th, 2013
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 5.95 KB | None | 0 0
  1. Imports System
  2. Imports System.Runtime.InteropServices
  3. Imports System.Drawing
  4. Imports System.Drawing.Imaging
  5.  
  6.  
  7. Namespace ScreenShot
  8.     '/ Provides functions to capture the entire screen, or a particular window, and save it to a file.
  9.     Public Class ScreenCapture
  10.         '/ Creates an Image object containing a screen shot of the entire desktop
  11.         Public Function CaptureScreen() As Image
  12.             Return CaptureWindow(User32.GetDesktopWindow())
  13.         End Function 'CaptureScreen
  14.         '/ Creates an Image object containing a screen shot of a specific window
  15.         Public Function CaptureWindow(ByVal handle As IntPtr) As Image
  16.             Dim SRCCOPY As Integer = &HCC0020
  17.             ' get te hDC of the target window
  18.             Dim hdcSrc As IntPtr = User32.GetWindowDC(handle)
  19.             ' get the size
  20.             Dim windowRect As New User32.RECT
  21.             User32.GetWindowRect(handle, windowRect)
  22.             Dim width As Integer = windowRect.right - windowRect.left
  23.             Dim height As Integer = windowRect.bottom - windowRect.top
  24.             ' create a device context we can copy to
  25.             Dim hdcDest As IntPtr = GDI32.CreateCompatibleDC(hdcSrc)
  26.             ' create a bitmap we can copy it to,
  27.             ' using GetDeviceCaps to get the width/height
  28.             Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, width, height)
  29.             ' select the bitmap object
  30.             Dim hOld As IntPtr = GDI32.SelectObject(hdcDest, hBitmap)
  31.             ' bitblt over
  32.             GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, SRCCOPY)
  33.             ' restore selection
  34.             GDI32.SelectObject(hdcDest, hOld)
  35.             ' clean up
  36.             GDI32.DeleteDC(hdcDest)
  37.             User32.ReleaseDC(handle, hdcSrc)
  38.  
  39.             ' get a .NET image object for it
  40.             Dim img As Image = Image.FromHbitmap(hBitmap)
  41.             ' free up the Bitmap object
  42.             GDI32.DeleteObject(hBitmap)
  43.  
  44.             Return img
  45.         End Function 'CaptureWindow
  46.         '/ Captures a screen shot of a specific window, and saves it to a file
  47.         Public Sub CaptureWindowToFile(ByVal handle As IntPtr, ByVal filename As String, ByVal format As ImageFormat)
  48.             Dim img As Image = CaptureWindow(handle)
  49.             img.Save(filename, format)
  50.         End Sub 'CaptureWindowToFile
  51.         '/ Captures a screen shot of the entire desktop, and saves it to a file
  52.         Public Sub CaptureScreenToFile(ByVal filename As String, ByVal format As ImageFormat)
  53.             Dim img As Image = CaptureScreen()
  54.             img.Save(filename, format)
  55.         End Sub 'CaptureScreenToFile
  56.         Public Function CaptureDeskTopRectangle(ByVal CapRect As Rectangle, ByVal CapRectWidth As Integer, ByVal CapRectHeight As Integer) As Bitmap
  57.             '/ Returns BitMap of the region of the desktop, similar to CaptureWindow, but can be used to
  58.             '/ create a snapshot of the desktop when no handle is present, by passing in a rectangle
  59.             '/ Grabs snapshot of entire desktop, then crops it using the passed in rectangle's coordinates
  60.             Dim SC As New ScreenShot.ScreenCapture
  61.             Dim bmpImage As New Bitmap(sc.CaptureScreen)
  62.             Dim bmpCrop As New Bitmap(CapRectWidth, CapRectHeight, bmpImage.PixelFormat)
  63.             Dim recCrop As New Rectangle(CapRect.X, CapRect.Y, CapRectWidth, CapRectHeight)
  64.             Dim gphCrop As Graphics = Graphics.FromImage(bmpCrop)
  65.             Dim recDest As New Rectangle(0, 0, CapRectWidth, CapRectHeight)
  66.             gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width, _
  67.               recCrop.Height, GraphicsUnit.Pixel)
  68.             Return bmpCrop
  69.         End Function
  70.         '/ Helper class containing Gdi32 API functions
  71.         Private Class GDI32
  72.             Public SRCCOPY As Integer = &HCC0020
  73.             ' BitBlt dwRop parameter
  74.             Declare Function BitBlt Lib "gdi32.dll" ( _
  75.                 ByVal hDestDC As IntPtr, _
  76.                 ByVal x As Int32, _
  77.                 ByVal y As Int32, _
  78.                 ByVal nWidth As Int32, _
  79.                 ByVal nHeight As Int32, _
  80.                 ByVal hSrcDC As IntPtr, _
  81.                 ByVal xSrc As Int32, _
  82.                 ByVal ySrc As Int32, _
  83.                 ByVal dwRop As Int32) As Int32
  84.  
  85.             Declare Function CreateCompatibleBitmap Lib "gdi32.dll" ( _
  86.                 ByVal hdc As IntPtr, _
  87.                 ByVal nWidth As Int32, _
  88.                 ByVal nHeight As Int32) As IntPtr
  89.  
  90.             Declare Function CreateCompatibleDC Lib "gdi32.dll" ( _
  91.                 ByVal hdc As IntPtr) As IntPtr
  92.  
  93.             Declare Function DeleteDC Lib "gdi32.dll" ( _
  94.                 ByVal hdc As IntPtr) As Int32
  95.  
  96.             Declare Function DeleteObject Lib "gdi32.dll" ( _
  97.                 ByVal hObject As IntPtr) As Int32
  98.  
  99.             Declare Function SelectObject Lib "gdi32.dll" ( _
  100.                 ByVal hdc As IntPtr, _
  101.                 ByVal hObject As IntPtr) As IntPtr
  102.         End Class 'GDI32
  103.         '/ Helper class containing User32 API functions
  104.         Public Class User32
  105.             <StructLayout(LayoutKind.Sequential)> _
  106.             Public Structure RECT
  107.                 Public left As Integer
  108.                 Public top As Integer
  109.                 Public right As Integer
  110.                 Public bottom As Integer
  111.             End Structure 'RECT
  112.  
  113.             Declare Function GetDesktopWindow Lib "user32.dll" () As IntPtr
  114.  
  115.             Declare Function GetWindowDC Lib "user32.dll" ( _
  116.                 ByVal hwnd As IntPtr) As IntPtr
  117.  
  118.             Declare Function ReleaseDC Lib "user32.dll" ( _
  119.                 ByVal hwnd As IntPtr, _
  120.                 ByVal hdc As IntPtr) As Int32
  121.  
  122.             Declare Function GetWindowRect Lib "user32.dll" ( _
  123.                 ByVal hwnd As IntPtr, _
  124.                 ByRef lpRect As RECT) As Int32
  125.  
  126.         End Class 'User32
  127.     End Class 'ScreenCapture
  128. End Namespace 'ScreenShot
Advertisement
Add Comment
Please, Sign In to add comment