Advertisement
Guest User

Untitled

a guest
May 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit On
  2. Option Strict On
  3.  
  4. Public Class Webcam
  5.  
  6.     Private Shared w32 As Win32Api
  7.     Private Shared Shadows [Handle] As IntPtr
  8.  
  9.     Public Shared Sub Start()
  10.  
  11.         w32 = New Win32Api(Hauptform.PicBox.Handle, 352, 287)
  12.         [Handle] = w32.GetCaptureHandle
  13.  
  14.     End Sub
  15.  
  16.     Public Shared Sub Beenden()
  17.         w32.MyHandle = [Handle]
  18.         If Not [Handle].Equals(IntPtr.Zero) Then
  19.             w32.DisposeConnection()
  20.         End If
  21.     End Sub
  22.  
  23.     Public Shared Sub ManuellSnapshot()
  24.         Dim Datum As String = My.Computer.Clock.LocalTime.Day & "." & My.Computer.Clock.LocalTime.Month & "." & My.Computer.Clock.LocalTime.Year
  25.         Dim Uhrzeit As String = My.Computer.Clock.LocalTime.Hour & ";" & My.Computer.Clock.LocalTime.Minute & ";" & My.Computer.Clock.LocalTime.Second
  26.         Dim Gesamt As String = Datum & Uhrzeit
  27.  
  28.         If Not [Handle].Equals(IntPtr.Zero) Then
  29.             w32.MyHandle = [Handle]
  30.             Dim img As Image = w32.GetImage()
  31.             If img IsNot Nothing Then
  32.                 Hauptform.PicBox.Image = img
  33.             End If
  34.         End If
  35.         IO.Directory.CreateDirectory("C:\Dokumente und Einstellungen\Chriz\Desktop\Buchhold Projekt\Snapshots\" & Date.Today & "\")
  36.         Hauptform.PicBox.Image.Save("C:\Dokumente und Einstellungen\Chriz\Desktop\Buchhold Projekt\Snapshots\" & Date.Today & "\" & Uhrzeit & ".jpg")
  37.  
  38.  
  39.     End Sub
  40.  
  41.     Public Shared Sub Snapshot()
  42.  
  43.      
  44.         Dim Datum As String = My.Computer.Clock.LocalTime.Day & "." & My.Computer.Clock.LocalTime.Month & "." & My.Computer.Clock.LocalTime.Year
  45.         Dim Uhrzeit As String = My.Computer.Clock.LocalTime.Hour & ";" & My.Computer.Clock.LocalTime.Minute & ";" & My.Computer.Clock.LocalTime.Second
  46.         Dim Gesamt As String = Datum & Uhrzeit
  47.  
  48.         If Not [Handle].Equals(IntPtr.Zero) Then
  49.             w32.MyHandle = [Handle]
  50.             Dim img As Image = w32.GetImage()
  51.             If img IsNot Nothing Then
  52.                 Hauptform.PicBox.Image = img
  53.             End If
  54.         End If
  55.         IO.Directory.CreateDirectory("C:\Dokumente und Einstellungen\Chriz\Desktop\Buchhold Projekt\Fehlerlogs\" & Date.Today & "\")
  56.         Hauptform.PicBox.Image.Save("C:\Dokumente und Einstellungen\Chriz\Desktop\Buchhold Projekt\Fehlerlogs\" & Date.Today & "\" & Uhrzeit & ".jpg")
  57.  
  58.  
  59.  
  60.     End Sub
  61. End Class
  62.  
  63. Public Class Win32Api
  64.  
  65. #Region "Api Functions"
  66.  
  67.     Private Declare Auto Function capCreateCaptureWindow Lib "avicap32.dll" ( _
  68.         ByVal lpszWindowName As String, _
  69.         ByVal dwStyle As Integer, _
  70.         ByVal x As Integer, _
  71.         ByVal y As Integer, _
  72.         ByVal nWidth As Integer, _
  73.         ByVal nHeight As Integer, _
  74.         ByVal hWnd As IntPtr, _
  75.         ByVal nID As Integer) _
  76.         As IntPtr
  77.  
  78.     Private Declare Auto Function SendMessage Lib "user32.dll" ( _
  79.         ByVal hwnd As IntPtr, _
  80.         ByVal uMsg As Integer, _
  81.         ByVal wParam As Integer, _
  82.         ByVal lParam As Integer) _
  83.         As Integer
  84.  
  85. #End Region
  86.  
  87. #Region "Constants"
  88.  
  89.     Private Const WM_USER As Int32 = &H400
  90.     Private Const WS_CHILD As Integer = &H40000000
  91.     Private Const WS_VISIBLE As Integer = &H10000000
  92.     Private Const WM_CAP_START As Integer = WM_USER
  93.     Private Const WM_CAP_DRIVER_CONNECT As Integer = (WM_CAP_START + 10)
  94.     Private Const WM_CAP_SET_PREVIEWRATE As Integer = (WM_CAP_START + 52)
  95.     Private Const WM_CAP_SET_OVERLAY As Integer = (WM_CAP_START + 51)
  96.     Private Const WM_CAP_SET_PREVIEW As Integer = (WM_CAP_START + 50)
  97.     Private Const WM_CAP_DRIVER_DISCONNECT As Integer = (WM_CAP_START + 11)
  98.     Private Const WM_CAP_EDIT_COPY As Integer = (WM_CAP_START + 30)
  99.  
  100. #End Region
  101.  
  102. #Region "Private"
  103.  
  104.     Private _hwnd As IntPtr
  105.     Private _width As Integer
  106.     Private _height As Integer
  107.  
  108. #End Region
  109.  
  110. #Region "Camera Id"
  111.  
  112.     Private Const CameraId As Integer = 0
  113.  
  114. #End Region
  115.  
  116. #Region "Frames"
  117.  
  118.     Private Const Frames As Integer = 24
  119.  
  120. #End Region
  121.  
  122. #Region "Positions"
  123.  
  124.     Private x As Integer = 0
  125.     Private y As Integer = 0
  126.  
  127. #End Region
  128.  
  129. #Region "Public"
  130.  
  131.     Public MyHandle As IntPtr
  132.  
  133. #End Region
  134.  
  135. #Region "Constructor"
  136.  
  137.     Public Sub New(ByVal hWnd As IntPtr, ByVal Width As Integer, ByVal Height As Integer)
  138.         If Not hWnd.Equals(IntPtr.Zero) Then
  139.             Me._hwnd = hWnd
  140.             If (Width >= 320) And (Height >= 240) Then
  141.                 Me._width = Width : Me._height = Height
  142.             Else
  143.                 Return
  144.             End If
  145.         Else
  146.             Return
  147.         End If
  148.     End Sub
  149.  
  150. #End Region
  151.  
  152. #Region "Functions"
  153.  
  154.     Public Function GetCaptureHandle() As IntPtr
  155.         Dim [Handle] As IntPtr = Win32Api.capCreateCaptureWindow("CaptureWindow", _
  156.             Win32Api.WS_CHILD + Win32Api.WS_VISIBLE, _
  157.             x, y, _
  158.             Me._width, Me._height, _
  159.             Me._hwnd, _
  160.             Win32Api.CameraId)
  161.         SendMessage([Handle], Win32Api.WM_CAP_DRIVER_CONNECT, Win32Api.CameraId, 0)
  162.         SendMessage([Handle], Win32Api.WM_CAP_SET_PREVIEWRATE, Win32Api.Frames, 0)
  163.         SendMessage([Handle], Win32Api.WM_CAP_SET_OVERLAY, 1, 0)
  164.         SendMessage([Handle], Win32Api.WM_CAP_SET_PREVIEW, 1, 0)
  165.         If Not [Handle].Equals(IntPtr.Zero) Then
  166.             Return [Handle]
  167.         Else
  168.             Return IntPtr.Zero
  169.         End If
  170.     End Function
  171.  
  172.     Public ReadOnly Property GetImage() As Drawing.Image
  173.         Get
  174.             Return Me.SetCurrentImageToClipBoard()
  175.         End Get
  176.     End Property
  177.  
  178.     Private Function SetCurrentImageToClipBoard() As Drawing.Image
  179.         Try
  180.             My.Computer.Clipboard.Clear()
  181.             SendMessage(Me.SetHandle(), Win32Api.WM_CAP_EDIT_COPY, 0, 0)
  182.             Dim img As Image = My.Computer.Clipboard.GetImage
  183.             If img IsNot Nothing Then
  184.                 Return img
  185.             Else
  186.                 Return Nothing
  187.             End If
  188.         Catch
  189.             Return Nothing
  190.         End Try
  191.     End Function
  192.  
  193.     Public Property SetHandle() As IntPtr
  194.         Get
  195.             Return MyHandle
  196.         End Get
  197.         Set(ByVal value As IntPtr)
  198.             MyHandle = value
  199.         End Set
  200.     End Property
  201.  
  202.     Public Sub DisposeConnection()
  203.         Dim result As Integer = SendMessage(Me.SetHandle(), Win32Api.WM_CAP_DRIVER_DISCONNECT, Win32Api.CameraId, 0)
  204.         Debug.WriteLine("Disconnected: " & result.ToString())
  205.     End Sub
  206.  
  207. #End Region
  208.  
  209. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement