[VB.NET] Webcam Programm Problem

11/03/2013 21:29 TheBeanjay#1
Hey Zusammen!

Ich arbeite im Moment an einem Programm dass das Bild der Webcam in einer PictureBox anzeigt und es speichern soll.

Mit Hilfe der avicap32.dll hab ich's geschafft dass das Bild meiner Webcam angezeigt wird aber hat jemand eine Idee wie Ich dass in eine Datei speichern könnte?

Hier ist mein Code:

Code:
Public Class Form1
    Dim videoHandle As IntPtr
    Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Declare Auto Function capCreateCaptureWindow Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Short, ByVal y As Integer, ByVal nWidth As Short, ByVal nHeight As Short, ByVal hWndParent As IntPtr, ByVal nID As Byte) As IntPtr

    Const EM_LINEFROMCHAR As Integer = &HC9
    Const EM_LINEINDEX As Integer = &HBB

    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 Short = WM_CAP_START + 30
    Const WM_CAP_DRIVER_CONNECT As Short = WM_CAP_START + 10
    Const WM_CAP_SET_PREVIEWRATE As Short = WM_CAP_START + 52
    Const WM_CAP_SET_OVERLAY As Short = WM_CAP_START + 51
    Const WM_CAP_SET_PREVIEW As Short = WM_CAP_START + 50
    Const WM_CAP_DRIVER_DISCONNECT As Short = WM_CAP_START + 11

    Function CreateCaptureWindow(ByRef hWndParent As IntPtr, Optional ByRef x As Short = 0, Optional ByRef y As Short = 0, Optional ByRef nWidth As Short = 640, Optional ByRef nHeight As Short = 420, Optional ByRef nCameraID As Integer = 0) As IntPtr
        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)
        SendMessage(previewHandle, WM_CAP_SET_PREVIEWRATE, 30, 0)
        SendMessage(previewHandle, WM_CAP_SET_OVERLAY, 1, 0)
        SendMessage(previewHandle, WM_CAP_SET_PREVIEW, 1, 0)

        Return previewHandle
    End Function

    Sub Disconnect(ByRef nCaptureHandle As IntPtr, Optional ByRef nCameraID As Integer = 0)
        SendMessage(nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, nCameraID, 0)
    End Sub

    Sub Form2_FormClosing() Handles Me.FormClosing
        Me.Disconnect(videoHandle)
    End Sub

    Sub Button1_Click() Handles Button1.Click
        videoHandle = Me.CreateCaptureWindow(PictureBox1.Handle)
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Disconnect(videoHandle)
    End Sub
End Class
11/03/2013 21:37 Ravenstorm#2
Einfach eine neue Funktion für das Speichern des Bilds aus der PictureBox deklarieren:

Code:
Private Sub Bild_Speichern()
        Try
            If (pictureBox1 IsNot Nothing) Then
                Dim dateiPfad As String = "C:\User\Desktop"
                Dim bm As Bitmap = pictureBox1 .Image
                bm.Save(dateiPfad & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
        Catch ex As Exception
            MessageBox.Show("Bild Wurde nicht gespeichert.", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
Bin mir nicht sicher ob die Funktion genau so richtig war, allerdings ist das die Standardvorgehensweise.
11/03/2013 21:41 TheBeanjay#3
Quote:
Originally Posted by Ravenstorm View Post
Einfach eine neue Funktion für das Speichern des Bilds aus der PictureBox deklarieren:

Code:
Private Sub Bild_Speichern()
        Try
            If (pictureBox1 IsNot Nothing) Then
                Dim dateiPfad As String = "C:\User\Desktop"
                Dim bm As Bitmap = pictureBox1 .Image
                bm.Save(dateiPfad & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
        Catch ex As Exception
            MessageBox.Show("Bild Wurde nicht gespeichert.", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
Bin mir nicht sicher ob die Funktion genau so richtig war, allerdings ist das die Standardvorgehensweise.
Ja aber ich will ja nicht ein Bild sondern ein Video wäre super ^^ Weil in meiner Picture Box ist ja auch nicht nur ein Bild sondern ein Preview der Webcam ^^ Also als Video nicht als Bild und jetzt ist nur die Frage wie ich dass speichern kann :D
11/03/2013 21:44 tolio#4
ein video ist nichts anderes als eine folge von bildern. ne direkte funktion das als video zu speichern ist mir nicht bekannt
11/03/2013 21:45 TheBeanjay#5
Kann man denn diese Vielen Bilder in ein Video zusammenfassen oder so? Damit ich am Ende des Tages ein Video und nicht 1000 Bilder habe?
11/03/2013 21:46 Ravenstorm#6
Quote:
Originally Posted by tolio View Post
ein video ist nichts anderes als eine folge von bildern. ne direkte funktion das als video zu speichern ist mir nicht bekannt
Ja vorallem weil er ist eine PictureBox schreibt... Es gab aber mal eine Libary die hat irgendwas in der Art gemacht von wegen Screens -> Video... Muss mich mal auf die Suche machen...
11/03/2013 21:49 TheBeanjay#7
Quote:
Originally Posted by Ravenstorm View Post
Ja vorallem weil er ist eine PictureBox schreibt... Es gab aber mal eine Libary die hat irgendwas in der Art gemacht von wegen Screens -> Video... Muss mich mal auf die Suche machen...
Das wär echt super nett ich hab auch gegooglet aber nichts gefunden ^^ Ich hoffe mal jemand der sich in VB.NET besser auskennt als Ich findet da was! :)
11/03/2013 21:52 tolio#8
ich hab grad mal "https://www.google.de/#q=vb.net+capture+webcam+video" gegooglet, da kommen dutzende sources...
11/03/2013 21:54 Frosttall#9
[Only registered and activated users can see links. Click Here To Register...]


Die Library die du benötigst: [Only registered and activated users can see links. Click Here To Register...]
11/03/2013 21:56 TheBeanjay#10
Quote:
Originally Posted by tolio View Post
ich hab grad mal "https://www.google.de/#q=vb.net+capture+webcam+video" gegooglet, da kommen dutzende sources...
Jup aber wird irgendwo dass Video auch gespeichert?? Ich hab keins gefunden ^^

Quote:
Originally Posted by Frosttall View Post
[Only registered and activated users can see links. Click Here To Register...]


Die Library die du benötigst: [Only registered and activated users can see links. Click Here To Register...]
Danke dass sieht schon mal vielversprechend aus mal gucken ob ich da was vernünftiges Zu Stande bringe :D

Ok ich hab jetzt erstmal versucht die einzelnen Bilder zu speichern aber noch nicht mal dass geht, anscheinend sieht VB.NET die PictureBox als leer an! Ich kriege auf jeden Fall immer eine Null Reference Exception. Ich hoffe ich habe keinen super simplen Fehler gemacht.

Hier nochmal der neue Source:

Code:
Public Class Form1
    Dim videoHandle As IntPtr
    Dim an As Boolean = False
    Declare Auto Function SendMessage Lib "user32" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Declare Auto Function capCreateCaptureWindow Lib "avicap32.dll" (ByVal lpszWindowName As String, ByVal dwStyle As Integer, ByVal x As Short, ByVal y As Integer, ByVal nWidth As Short, ByVal nHeight As Short, ByVal hWndParent As IntPtr, ByVal nID As Byte) As IntPtr

    Const EM_LINEFROMCHAR As Integer = &HC9
    Const EM_LINEINDEX As Integer = &HBB

    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 Short = WM_CAP_START + 30
    Const WM_CAP_DRIVER_CONNECT As Short = WM_CAP_START + 10
    Const WM_CAP_SET_PREVIEWRATE As Short = WM_CAP_START + 52
    Const WM_CAP_SET_OVERLAY As Short = WM_CAP_START + 51
    Const WM_CAP_SET_PREVIEW As Short = WM_CAP_START + 50
    Const WM_CAP_DRIVER_DISCONNECT As Short = WM_CAP_START + 11

    Function CreateCaptureWindow(ByRef hWndParent As IntPtr, Optional ByRef x As Short = 0, Optional ByRef y As Short = 0, Optional ByRef nWidth As Short = 640, Optional ByRef nHeight As Short = 420, Optional ByRef nCameraID As Integer = 0) As IntPtr
        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)
        SendMessage(previewHandle, WM_CAP_SET_PREVIEWRATE, 30, 0)
        SendMessage(previewHandle, WM_CAP_SET_OVERLAY, 1, 0)
        SendMessage(previewHandle, WM_CAP_SET_PREVIEW, 1, 0)

        Return previewHandle
    End Function

    Sub Disconnect(ByRef nCaptureHandle As IntPtr, Optional ByRef nCameraID As Integer = 0)
        SendMessage(nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, nCameraID, 0)
    End Sub

    Sub Form2_FormClosing() Handles Me.FormClosing
        Me.Disconnect(videoHandle)
    End Sub

    Sub Button1_Click() Handles Button1.Click
        an = True
        videoHandle = Me.CreateCaptureWindow(PictureBox1.Handle)
        While an = True
            Bild_Speichern()

        End While

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        an = False
        Me.Disconnect(videoHandle)
    End Sub
    Private Sub Bild_Speichern()
        Try
            If (pictureBox1 IsNot Nothing) Then
                Dim BildAlsDateiPfad As String = "C:\User\Desktop\Hallo"
                Dim bm As Bitmap = pictureBox1.Image
                bm.Save(BildAlsDateiPfad & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            End If
        Catch ex As Exception
            MsgBox("Bild Wurde nicht gespeichert.", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class
Ich hab ja jetzt nicht wirklich viel geändert ^^


Danke für eure Hilfe jetzt schon mal :)
11/03/2013 22:41 Ravenstorm#11
Kannst du mir sagen was die Funktion genau macht hier:

Code:
videoHandle = Me.CreateCaptureWindow(PictureBox1.Handle)
weil mir scheint als ersetzt du das bild der pictureBox nicht wirklich..
11/04/2013 15:23 'Heaven.#12
[Only registered and activated users can see links. Click Here To Register...]


Nutz nen Converter, falls dir C# nicht geläufig ist
11/04/2013 17:45 TheBeanjay#13
Quote:
Originally Posted by Ravenstorm View Post
Kannst du mir sagen was die Funktion genau macht hier:

Code:
videoHandle = Me.CreateCaptureWindow(PictureBox1.Handle)
weil mir scheint als ersetzt du das bild der pictureBox nicht wirklich..
Ich vermute dass es kein Bild ist was in der PictureBox angezeigt wird sondern ein Video Stream oder so :/
11/08/2013 19:06 TheBeanjay#14
Hat noch jemand Ideen?
11/10/2013 17:50 TheBeanjay#15
Niemand mehr? Bitte wär schön wenn jemand ne Lösung wüsste :)