PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ScreenCapture.vb

http://bookmarksavevb6.codeplex.com
Visual Basic | 254 lines | 125 code | 42 blank | 87 comment | 0 complexity | d8d280c843f504ac93daf198ad2abaed MD5 | raw file
  1. Imports System
  2. Imports System.Runtime.InteropServices
  3. Imports System.Drawing
  4. Imports System.Drawing.Imaging
  5. Namespace ScreenShot
  6. ''' <summary>
  7. ''' Simple functions for screen captures in VB
  8. ''' From a post here
  9. ''' http://www.vbforums.com/showthread.php?t=385497
  10. ''' Adapted and extended slightly by Darin Higgins Oct, 2011
  11. ''' </summary>
  12. ''' <remarks></remarks>
  13. Public Class Capture
  14. ''' <summary>
  15. ''' Captures the primary monitor screen
  16. ''' </summary>
  17. ''' <returns></returns>
  18. ''' <remarks></remarks>
  19. Public Shared Function Screen() As Image
  20. Return Window(User32.GetDesktopWindow())
  21. End Function
  22. ''' <summary>
  23. ''' Captures the entire desktop when multiple monitors are present
  24. ''' </summary>
  25. ''' <returns></returns>
  26. ''' <remarks></remarks>
  27. Public Shared Function EntireDesktop() As Image
  28. Dim base As IntPtr = User32.FindWindow("Progman", "Program Manager")
  29. Dim b2 As IntPtr = User32.FindWindowEx(base, vbNullString, "SHELLDLL_DefView", vbNullString)
  30. b2 = User32.FindWindowEx(b2, vbNullString, "SysListView32", vbNullString)
  31. Return Window(b2)
  32. End Function
  33. ''' <summary>
  34. ''' Return image of a specific window
  35. ''' </summary>
  36. ''' <param name="hwnd"></param>
  37. ''' <returns></returns>
  38. ''' <remarks></remarks>
  39. Public Shared Function Window(ByVal hwnd As IntPtr) As Image
  40. Dim wRect As New User32.RECT
  41. User32.GetWindowRect(hwnd, wRect)
  42. Dim Rect = New Rectangle(0, 0, wRect.right - wRect.left, wRect.bottom - wRect.top)
  43. Return windowRect(hwnd, Rect)
  44. End Function
  45. ''' <summary>
  46. ''' Retrieves a rectangle (in window's client coords) from the target window
  47. ''' </summary>
  48. ''' <param name="hwnd"></param>
  49. ''' <param name="Rect"></param>
  50. ''' <returns></returns>
  51. ''' <remarks></remarks>
  52. Public Shared Function ClientWindowRect(ByVal hwnd As IntPtr, ByVal Rect As Rectangle) As Image
  53. '---- the rectangle is in client coords
  54. ' so add in the NC left and top to adjust it to window coords
  55. '---- get 0,0 client coords in screen coords
  56. Dim pt As User32.POINT
  57. pt.X = 0 : pt.Y = 0
  58. User32.ClientToScreen(hwnd, pt)
  59. '---- get the window rect (including NC area) in screen coords
  60. Dim wRect As New User32.RECT
  61. User32.GetWindowRect(hwnd, wRect)
  62. '---- to convert the Rect from Client Coords into Window Coords
  63. Rect.Offset(pt.X - wRect.left, pt.Y - wRect.top)
  64. '---- and capture based on the WINDOW coords
  65. Return WindowRect(hwnd, Rect)
  66. End Function
  67. ''' <summary>
  68. ''' Return shot of a portion of a specific window
  69. ''' </summary>
  70. ''' <param name="hwnd"></param>
  71. ''' <param name="Rect"></param>
  72. ''' <returns></returns>
  73. ''' <remarks></remarks>
  74. Public Shared Function WindowRect(ByVal hwnd As IntPtr, ByVal Rect As Rectangle) As Image
  75. Dim SRCCOPY As Integer = &HCC0020
  76. ' get te hDC of the target window
  77. Dim hdcSrc As IntPtr = User32.GetWindowDC(hwnd)
  78. ' get the size
  79. ' create a device context we can copy to
  80. Dim hdcDest As IntPtr = GDI32.CreateCompatibleDC(hdcSrc)
  81. ' create a bitmap we can copy it to,
  82. ' using GetDeviceCaps to get the width/height
  83. Dim hBitmap As IntPtr = GDI32.CreateCompatibleBitmap(hdcSrc, Rect.Width, Rect.Height)
  84. ' select the bitmap object
  85. Dim hOld As IntPtr = GDI32.SelectObject(hdcDest, hBitmap)
  86. ' bitblt over
  87. GDI32.BitBlt(hdcDest, 0, 0, Rect.Width, Rect.Height, hdcSrc, Rect.Left, Rect.Top, SRCCOPY)
  88. ' restore selection
  89. GDI32.SelectObject(hdcDest, hOld)
  90. ' clean up
  91. GDI32.DeleteDC(hdcDest)
  92. User32.ReleaseDC(hwnd, hdcSrc)
  93. ' get a .NET image object for it
  94. Dim img As Image = Image.FromHbitmap(hBitmap)
  95. ' free up the Bitmap object
  96. GDI32.DeleteObject(hBitmap)
  97. Return img
  98. End Function
  99. ''' <summary>
  100. ''' Capture shot of a window and save to a file
  101. ''' </summary>
  102. ''' <param name="hwnd"></param>
  103. ''' <param name="filename"></param>
  104. ''' <param name="format"></param>
  105. ''' <remarks></remarks>
  106. Public Shared Sub WindowToFile(ByVal hwnd As IntPtr, ByVal filename As String, ByVal format As ImageFormat)
  107. Dim img As Image = Window(hwnd)
  108. img.Save(filename, format)
  109. End Sub
  110. ''' <summary>
  111. ''' Capture shot of primary desktop monitor and save to file
  112. ''' </summary>
  113. ''' <param name="filename"></param>
  114. ''' <param name="format"></param>
  115. ''' <remarks></remarks>
  116. Public Shared Sub ScreenToFile(ByVal filename As String, ByVal format As ImageFormat)
  117. Dim img As Image = Screen()
  118. img.Save(filename, format)
  119. End Sub
  120. ''' <summary>
  121. ''' Capture a rectangular portion of the desktop
  122. ''' </summary>
  123. ''' <param name="CapRect"></param>
  124. ''' <param name="CapRectWidth"></param>
  125. ''' <param name="CapRectHeight"></param>
  126. ''' <returns></returns>
  127. ''' <remarks></remarks>
  128. Public Function DeskTopRect(ByVal CapRect As Rectangle, ByVal CapRectWidth As Integer, ByVal CapRectHeight As Integer) As Bitmap
  129. '/ Returns BitMap of the region of the desktop, similar to CaptureWindow, but can be used to
  130. '/ create a snapshot of the desktop when no handle is present, by passing in a rectangle
  131. '/ Grabs snapshot of entire desktop, then crops it using the passed in rectangle's coordinates
  132. Dim bmpImage As New Bitmap(Screen())
  133. Dim bmpCrop As New Bitmap(CapRectWidth, CapRectHeight, bmpImage.PixelFormat)
  134. Dim recCrop As New Rectangle(CapRect.X, CapRect.Y, CapRectWidth, CapRectHeight)
  135. Dim gphCrop As Graphics = Graphics.FromImage(bmpCrop)
  136. Dim recDest As New Rectangle(0, 0, CapRectWidth, CapRectHeight)
  137. gphCrop.DrawImage(bmpImage, recDest, recCrop.X, recCrop.Y, recCrop.Width, _
  138. recCrop.Height, GraphicsUnit.Pixel)
  139. Return bmpCrop
  140. End Function
  141. ''' <summary>
  142. ''' Helper GDI p/invoke functions
  143. ''' </summary>
  144. ''' <remarks></remarks>
  145. Private Class GDI32
  146. Public SRCCOPY As Integer = &HCC0020
  147. ' BitBlt dwRop parameter
  148. Declare Function BitBlt Lib "gdi32.dll" ( _
  149. ByVal hDestDC As IntPtr, _
  150. ByVal x As Int32, _
  151. ByVal y As Int32, _
  152. ByVal nWidth As Int32, _
  153. ByVal nHeight As Int32, _
  154. ByVal hSrcDC As IntPtr, _
  155. ByVal xSrc As Int32, _
  156. ByVal ySrc As Int32, _
  157. ByVal dwRop As Int32) As Int32
  158. Declare Function CreateCompatibleBitmap Lib "gdi32.dll" ( _
  159. ByVal hdc As IntPtr, _
  160. ByVal nWidth As Int32, _
  161. ByVal nHeight As Int32) As IntPtr
  162. Declare Function CreateCompatibleDC Lib "gdi32.dll" ( _
  163. ByVal hdc As IntPtr) As IntPtr
  164. Declare Function DeleteDC Lib "gdi32.dll" ( _
  165. ByVal hdc As IntPtr) As Int32
  166. Declare Function DeleteObject Lib "gdi32.dll" ( _
  167. ByVal hObject As IntPtr) As Int32
  168. Declare Function SelectObject Lib "gdi32.dll" ( _
  169. ByVal hdc As IntPtr, _
  170. ByVal hObject As IntPtr) As IntPtr
  171. End Class 'GDI32
  172. ''' <summary>
  173. ''' Helper User32 p/Invoke functions
  174. ''' </summary>
  175. ''' <remarks></remarks>
  176. Private Class User32
  177. <StructLayout(LayoutKind.Sequential)> _
  178. Public Structure RECT
  179. Public left As Integer
  180. Public top As Integer
  181. Public right As Integer
  182. Public bottom As Integer
  183. End Structure 'RECT
  184. <StructLayout(LayoutKind.Sequential)> _
  185. Public Structure POINT
  186. Public X As Integer
  187. Public Y As Integer
  188. End Structure
  189. Declare Function GetDesktopWindow Lib "user32.dll" () As IntPtr
  190. Declare Function GetWindowDC Lib "user32.dll" ( _
  191. ByVal hwnd As IntPtr) As IntPtr
  192. Declare Function ReleaseDC Lib "user32.dll" ( _
  193. ByVal hwnd As IntPtr, _
  194. ByVal hdc As IntPtr) As Int32
  195. Declare Function GetWindowRect Lib "user32.dll" ( _
  196. ByVal hwnd As IntPtr, _
  197. ByRef lpRect As RECT) As Int32
  198. Declare Function FindWindowEx Lib "User32.dll" ( _
  199. ByVal hwndParent As IntPtr, _
  200. ByVal hwndChild As IntPtr, _
  201. ByVal className As String, _
  202. ByVal caption As String) As IntPtr
  203. Declare Function FindWindow Lib "User32.dll" ( _
  204. ByVal className As String, _
  205. ByVal caption As String) As IntPtr
  206. Declare Function ScreenToClient Lib "user32.dll" ( _
  207. ByVal hWnd As IntPtr, ByRef pt As POINT) As Integer
  208. Declare Function ClientToScreen Lib "user32.dll" ( _
  209. ByVal hWnd As IntPtr, ByRef pt As POINT) As Integer
  210. End Class
  211. End Class
  212. End Namespace