Imports System.IO
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Public Class Form1
Dim mybild As Bitmap
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByRef pDst As Byte, ByRef pSrc As Byte, ByVal ByteLen As Integer)
Public Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Const EM_LINEFROMCHAR As Integer = &HC9
Const EM_LINEINDEX As Integer = &HBB
Public Declare Auto Function capCreateCaptureWindow 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 Integer, ByVal hWndParent As IntPtr, ByVal nID As Integer) As IntPtr
Const WS_CHILD As Integer = &H40000000
Const WS_VISIBLE As Integer = &H10000000
Const WM_USER As Short = &H400S
Const WM_CAP_START As Short = &H400S
Const WM_CAP_EDIT_COPY As Integer = (WM_CAP_START + 30)
Const WM_CAP_DRIVER_CONNECT As Integer = (WM_CAP_START + 10)
Const WM_CAP_SET_PREVIEWRATE As Integer = (WM_CAP_START + 52)
Const WM_CAP_SET_OVERLAY As Integer = (WM_CAP_START + 51)
Const WM_CAP_SET_PREVIEW As Integer = (WM_CAP_START + 50)
Const WM_CAP_DRIVER_DISCONNECT As Integer = (WM_CAP_START + 11)
'Um den Aufruf der Capture Funktionen noch einfacher zu machen braucht man noch sog. Wrapper Funktionen.
'Diese sind ganz einfach aufzurufen und übernehmen den kompilzierteren Aufruf der API Methoden.
'Hier werden 3 erstellt:
'CreateCaptureWindow - Zeigt das WebCam Bild auf einem Control an
'CapturePicture - Erstellt einen Schnappschuss und gibt ihn als System.Drawing.Image zurück.
'Disconnect - Gibt die für die WebCam benötigten Resourcen frei
' Handle für die WebCam.
Dim videoHandle As System.IntPtr
' senden eienr Ausgabenvorschau der WebCam an das betreuende control hWndParent.
Public Function CreateCaptureWindow(ByRef hWndParent As IntPtr, Optional ByRef x As Integer = 0, Optional ByRef y As Integer = 0, Optional ByRef nWidth As Integer = 1280, Optional ByRef nHeight As Integer = 720, Optional ByRef nCameraID As Integer = 0) As IntPtr 'Hier wird die Cam eingestellt 1 für intern 0 extern
Dim previewHandle As IntPtr
previewHandle = capCreateCaptureWindow("Video", WS_CHILD + WS_VISIBLE, x, y, nWidth, nHeight, hWndParent, 1)
SendMessage(previewHandle, WM_CAP_DRIVER_CONNECT, nCameraID, 0) '1
SendMessage(previewHandle, WM_CAP_SET_PREVIEWRATE, 15, 0)
SendMessage(previewHandle, WM_CAP_SET_OVERLAY, 1, 0)
SendMessage(previewHandle, WM_CAP_SET_PREVIEW, 1, 0)
Return previewHandle
End Function
Public Function CapturePicture(ByRef nCaptureHandle As IntPtr) As System.Drawing.Image
My.Computer.Clipboard.Clear()
SendMessage(nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0)
Return My.Computer.Clipboard.GetImage
End Function
Public Sub Disconnect(ByRef nCaptureHandle As IntPtr, Optional ByRef nCameraID As Integer = 0)
SendMessage(nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, nCameraID, 0) '1
End Sub
'Das wars! Die API wird von den Wrapper Funktionen sauber aufgerufen und die WebCam steht zur Verfügung.
'Um das Bild nun z.B. in einer PictureBox anzuzeigen genügt folgender Code:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
videoHandle = Me.CreateCaptureWindow(Me.picLive.Handle)
End Sub
Private Sub btnBildUebernehmen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBildUebernehmen.Click
Dim BILD As Bitmap
BILD = CapturePicture(videoHandle)
picSchwarzWeiß.Image = BILD
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Text = Now
End Sub
Private Sub btnSchwerzWeiß_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSchwerzWeiß.Click
' Wandelt das Bild in ein Graustufenbild um
' Gewichtung der Farben Luminanz-Umsetzung
Dim x, y As Integer
Dim Farbe As Color
Dim FarbeGrau As Color
Dim Grauwert As Integer
Dim Bild As Bitmap = picSchwarzWeiß.Image
Dim BildGrau As New Bitmap(Bild.Width, Bild.Height)
For y = 0 To Bild.Height - 1
For x = 0 To Bild.Width - 1
Farbe = Bild.GetPixel(x, y)
Grauwert = Farbe.R * 0.3 + Farbe.G * 0.59 + Farbe.B * 0.11 '0.3 0.59 0.11
FarbeGrau = Color.FromArgb(Grauwert, Grauwert, Grauwert)
BildGrau.SetPixel(x, y, FarbeGrau)
Next
Next
' Jetzt GrauBild in PictureBox anzeigen
picSchwarzWeiß.Image = BildGrau
End Sub
End Class