1. Option Explicit On
  2.  
  3. Public Class Cam_Base
  4. #Region "Api/constants"
  5.  
  6. Private Const WS_CHILD As Integer = &H40000000
  7. Private Const WS_VISIBLE As Integer = &H10000000
  8. Private Const SWP_NOMOVE As Short = &H2S
  9. Private Const SWP_NOZORDER As Short = &H4S
  10. Private Const WM_USER As Short = &H400S
  11. Private Const WM_CAP_DRIVER_CONNECT As Integer = WM_USER + 10
  12. Private Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_USER + 11
  13. Private Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_USER + 45
  14. Private Const WM_CAP_SET_PREVIEW As Integer = WM_USER + 50
  15. Private Const WM_CAP_SET_PREVIEWRATE As Integer = WM_USER + 52
  16. Private Const WM_CAP_GRAB_FRAME_NOSTOP As Integer = WM_USER + 61
  17. Private Const WM_CAP_EDIT_COPY As Integer = WM_USER + 30
  18. Private Const WM_CAP_GET_FRAME As Long = 1084
  19. Private Const WM_CAP_COPY As Long = 1054
  20. Private Const WM_CAP_START As Long = WM_USER
  21. Private Const WM_CAP_STOP As Long = (WM_CAP_START + 68)
  22. Private Const WM_CAP_SEQUENCE As Long = (WM_CAP_START + 62)
  23. Private Const WM_CAP_SET_SEQUENCE_SETUP As Long = (WM_CAP_START + 64)
  24. Private Const WM_CAP_FILE_SET_CAPTURE_FILEA As Long = (WM_CAP_START + 20)
  25.  
  26. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Short, ByVal lParam As String) As Integer
  27. Private Declare Function capCreateCaptureWindowA Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Short, ByVal hWndParent As Integer, ByVal nID As Integer) As Integer
  28. Private Declare Function capGetDriverDescriptionA Lib "avicap32.dll" (ByVal wDriver As Short, ByVal lpszName As String, ByVal cbName As Integer, ByVal lpszVer As String, ByVal cbVer As Integer) As Boolean
  29. Private Declare Function BitBlt Lib "GDI32.DLL" (ByVal hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As Int32) As Boolean
  30.  
  31. #End Region
  32.  
  33. Private iDevice As String
  34. Private hHwnd As Integer
  35. Private lwndC As Integer
  36.  
  37. Public iRunning As Boolean
  38.  
  39. Private CamFrameRate As Integer = 15
  40. Private OutputHeight As Integer = 240
  41. Private OutputWidth As Integer = 360
  42.  
  43. Public Sub New(ByVal control As Windows.Forms.Control)
  44. Me.initCam(control.Handle.ToInt32())
  45. End Sub
  46.  
  47. Public Sub New()
  48. 'Do nothing, wait for user to set up manualy
  49. End Sub
  50.  
  51. Public Sub resetCam()
  52. 'resets the camera after setting change
  53. If iRunning Then
  54. closeCam()
  55. Application.DoEvents()
  56.  
  57. If setCam() = False Then
  58. MessageBox.Show("Errror Setting/Re-Setting Camera")
  59. End If
  60. End If
  61.  
  62. End Sub
  63.  
  64. Public Sub initCam(ByVal parentH As Integer)
  65. 'Gets the handle and initiates camera setup
  66. If Me.iRunning = True Then
  67. MessageBox.Show("Camera Is Already Running")
  68. Exit Sub
  69. Else
  70.  
  71. hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, OutputWidth, CShort(OutputHeight), parentH, 0)
  72.  
  73.  
  74. If setCam() = False Then
  75. MessageBox.Show("Error setting Up Camera")
  76. End If
  77. End If
  78. End Sub
  79.  
  80. Public Sub setFrameRate(ByVal iRate As Long)
  81. 'sets the frame rate of the camera
  82. CamFrameRate = CInt(1000 / iRate)
  83.  
  84. resetCam()
  85.  
  86. End Sub
  87.  
  88. Private Function setCam() As Boolean
  89. 'Sets all the camera up
  90. If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, CShort(iDevice), CType(0, String)) = 1 Then
  91. SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, CShort(CamFrameRate), CType(0, String))
  92. SendMessage(hHwnd, WM_CAP_SET_PREVIEW, 1, CType(0, String))
  93. Me.iRunning = True
  94. Return True
  95. Else
  96. Me.iRunning = False
  97. Return False
  98. End If
  99. End Function
  100.  
  101. Public Function closeCam() As Boolean
  102. 'Closes the camera
  103. If Me.iRunning Then
  104. closeCam = CBool(SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, 0, CType(0, String)))
  105. Me.iRunning = False
  106. End If
  107. End Function
  108.  
  109. Public Function copyFrame(ByVal scr As PictureBox) As Bitmap
  110. Return Me.copyFrame(scr, New RectangleF(0, 0, scr.Width, scr.Height))
  111. End Function
  112.  
  113. Public Function copyFrame(ByVal src As PictureBox, ByVal rect As RectangleF) As Bitmap
  114. If iRunning Then
  115. Dim srcPic As Graphics = src.CreateGraphics
  116. Dim srcBmp As New Bitmap(src.Width, src.Height, srcPic)
  117. Dim srcMem As Graphics = Graphics.FromImage(srcBmp)
  118.  
  119.  
  120. Dim HDC1 As IntPtr = srcPic.GetHdc
  121. Dim HDC2 As IntPtr = srcMem.GetHdc
  122.  
  123. BitBlt(HDC2, 0, 0, CInt(rect.Width), _
  124. CInt(rect.Height), HDC1, CInt(rect.X), CInt(rect.Y), 13369376)
  125.  
  126. copyFrame = CType(srcBmp.Clone(), Bitmap)
  127.  
  128. 'Clean Up
  129. srcPic.ReleaseHdc(HDC1)
  130. srcMem.ReleaseHdc(HDC2)
  131. srcPic.Dispose()
  132. srcMem.Dispose()
  133. Else
  134. MessageBox.Show("Camera Is Not Running!")
  135. Return New Bitmap(0, 0)
  136. End If
  137. End Function
  138.  
  139. Public Function getBitmap() As Bitmap
  140. Dim data As IDataObject
  141. Dim bmap As Image
  142. ' Copy image to clipboard
  143. SendMessage(hHwnd, WM_CAP_GRAB_FRAME_NOSTOP, 0, 0)
  144. SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
  145.  
  146. ' Get image from clipboard and convert it to a bitmap
  147. data = Clipboard.GetDataObject()
  148. If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
  149. bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
  150. Return bmap
  151. Else
  152. Return New Bitmap(0, 0)
  153. End If
  154. End Function
  155.  
  156. Public Function FPS() As Integer
  157. Return CInt(1000 / (CamFrameRate))
  158. End Function
  159.  
  160. End Class
  161.  
Tweet This!