Video in Picture Box

  • VB6

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von sasasavic.

    Video in Picture Box

    Hi,

    ich suche einen Weg, ein Bild (also eher Video) von einer TV-Karte oder einer Webcam z.B. in eine Picture Box zu bekommen.

    Es muss nich total verzögerungsfrei sein, es kann auch etwas "ruckeln" (ist also ok, wenn das bild etwa alle 0,1 Sekunden aktualisiert wird).
    Wäre aber eigentlich auch nicht schlecht, wenn es normal schnell (ca. 24 Bilder/Sek) wäre!
    also ich habe etwas, um bilder von ner webcam zu machen (von vb-archiv.de)

    Option Explicit

    ' benötigte Deklarationen
    Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
    Alias "capCreateCaptureWindowA" ( _
    ByVal lpszWindowName As String, _
    ByVal dwStyle As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal hWndParent As Long, _
    ByVal nID As Long) As Long

    Private Const WS_CHILD = &H40000000
    Private Const WS_VISIBLE = &H10000000
    Private Const WM_USER = &H400
    Private Const WM_CAP_START = &H400
    Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
    Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)
    Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)
    Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)
    Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)
    Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)

    Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

    Private Preview_Handle As Long
    Public Function CreateCaptureWindow( _
    hWndParent As Long, _
    Optional x As Long = 0, _
    Optional y As Long = 0, _
    Optional nWidth As Long = 320, _
    Optional nHeight As Long = 240, _
    Optional nCameraID As Long = 0) As Long

    Preview_Handle = capCreateCaptureWindow("Video", _
    WS_CHILD + WS_VISIBLE, x, y, _
    nWidth, nHeight, hWndParent, 1)

    SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, nCameraID, 0
    SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 30, 0
    SendMessage Preview_Handle, WM_CAP_SET_OVERLAY, 1, 0
    SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0

    CreateCaptureWindow = Preview_Handle
    End Function
    Public Sub CapturePicture(nCaptureHandle As Long, _
    picCapture As PictureBox)

    Clipboard.Clear
    SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0
    picCapture.Picture = Clipboard.GetData
    End Sub
    Public Sub Disconnect(nCaptureHandle As Long, _
    Optional nCameraID = 0)

    SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, _
    nCameraID, 0
    End Sub
    Noch ein paar Erläuterungen zum Code:
    In der Funktion "CreateCaptureWindow" bestimmen die Parameter nWidth und nHeight die Auflösung des Kamerabildes. Die Funktion gibt das "Video-Handle" zurück, welches für alles weitere benötigt wird. Mit "CapturePicture" wird das aktuelle Bild in die Zwischenablage kopiert und in eine Picturebox eingefügt. Und noch was: Wenn Sie mehr als nur eine Kamera am PC hängen haben, können Sie mit dem Parameter nCameraID die Nummer der Kamera angeben. Wenn Sie nur eine am Rechner hängen haben, müssen Sie dort eigentlich immer 0 angeben.

    Beispiel für einen Test:
    Starten Sie ein neues Projekt und setzen die Höhe der Form auf 4600 Twips. Nun fügen Sie einen CommandButton am untere Ende der Form ein. Den nennen Sie "cmdSave". Ziehen Sie noch eine PictureBox auf die Programmoberfläche. Deren Height-Eigenschaft setzen Sie auf 3600 und die Width-Eigenschaft auf 4800.

    Fügen Sie den obigen Code in ein Modul ein. Dann öffnen Sie per Doppelklick auf die Form das Code-Fenster. Dort fügen Sie folgenden Code ein:

    Option Explicit

    Dim Video_Handle as Long
    Private Sub Form_Load()
    Picture1.Visible = False
    Video_Handle = CreateCaptureWindow(Me.hwnd)
    End Sub
    Private Sub cmdSave_Click()
    CapturePicture Video_Handle, Picture1
    SavePicture Picture1.Picture, "C:\Test.bmp"
    End Sub
    Starten Sie das Programm. Wenn alles glatt läuft, müssten Sie Das Live-Bild der Video-Kamera auf der Form sehen und per Klick auf "Command1" das Bild unter "C:\Test.bmp" abspeichern können.



    Felix Kröger
    Ich hab auch noch n Code, um AVIs in ner PicBox abzuspielen, du hattest in deiner Überschrift was davon erwähnt. Wenn du den auch haben willst:

    Option Explicit

    ' benötigte API-Deklarationen
    Private Declare Function mciSendString Lib "winmm.dll" _
    Alias "mciSendStringA" ( _
    ByVal lpstrCommand As String, _
    ByVal lpstrReturnString As String, _
    ByVal uReturnLength As Long, _
    ByVal hwndCallback As Long) As Long

    Private Declare Function GetShortPathName Lib "kernel32" _
    Alias "GetShortPathNameA" ( _
    ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, _
    ByVal cchBuffer As Long) As Long
    ' AVI-File öffnen
    Private Sub AVI_Open(ByVal sFile As String)
    Dim sBuffer As String * 256
    Dim nResult As Long

    ' DOS-Namen ermitteln (8.3-Format)
    nResult = GetShortPathName(sFile, sBuffer, Len(sBuffer))
    sFile = Left$(sBuffer, nResult)

    ' AVI öffnen
    ' öffnet ein Gerät und eine Multimedia-Datei
    mciSendString "open " & sFile & " type avivideo" & _
    " alias myAVI", 0, 0, 0

    ' Anzeige in der PictureBox
    mciSendString "window myAVI handle " & _
    CStr(Picture1.hWnd), 0, 0, 0
    End Sub

    ' AVI in PictureBox abspielen
    Private Sub AVI_Play()
    mciSendString "play myAVI from 0", 0, 0, 0
    End Sub

    ' Abspielvorgang stoppen
    Private Sub AVI_Stop()
    mciSendString "close myAVI", 0, 0, 0
    End Sub

    ' MCI Schließen
    Private Function AVI_Close()
    mciSendString "close myAVI", 0, 0, 0
    End Function
    ' AVI öffnen und abspielen
    Private Sub cmdPlay_Click()
    Dim sFile As String

    ' Dateiname
    sFile = "c:\winnt\clock.avi"

    ' MCI öffnen
    AVI_Open sFile

    ' Abspielvorgang starten
    AVI_Play
    End Sub
    ' Abspielvorgang beenden und MCI schließen
    Private Sub cmdStop_Click()
    AVI_Stop
    AVI_Close
    End Sub


    Felix Kröger
    Kröger schrieb am 03.07.2004 23:30
    also ich habe etwas, um bilder von ner webcam zu machen (von vb-archiv.de)

    Option Explicit

    ' benötigte Deklarationen
    Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
    Alias "capCreateCaptureWindowA" ( _
    ByVal lpszWindowName As String, _
    ByVal dwStyle As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal hWndParent As Long, _
    ByVal nID As Long) As Long

    Private Const WS_CHILD = &H40000000
    Private Const WS_VISIBLE = &H10000000
    Private Const WM_USER = &H400
    Private Const WM_CAP_START = &H400
    Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
    Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)
    Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)
    Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)
    Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)
    Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)

    Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

    Private Preview_Handle As Long
    Public Function CreateCaptureWindow( _
    hWndParent As Long, _
    Optional x As Long = 0, _
    Optional y As Long = 0, _
    Optional nWidth As Long = 320, _
    Optional nHeight As Long = 240, _
    Optional nCameraID As Long = 0) As Long

    Preview_Handle = capCreateCaptureWindow("Video", _
    WS_CHILD + WS_VISIBLE, x, y, _
    nWidth, nHeight, hWndParent, 1)

    SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, nCameraID, 0
    SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 30, 0
    SendMessage Preview_Handle, WM_CAP_SET_OVERLAY, 1, 0
    SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0

    CreateCaptureWindow = Preview_Handle
    End Function
    Public Sub CapturePicture(nCaptureHandle As Long, _
    picCapture As PictureBox)

    Clipboard.Clear
    SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0
    picCapture.Picture = Clipboard.GetData
    End Sub
    Public Sub Disconnect(nCaptureHandle As Long, _
    Optional nCameraID = 0)

    SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, _
    nCameraID, 0
    End Sub
    Noch ein paar Erläuterungen zum Code:
    In der Funktion "CreateCaptureWindow" bestimmen die Parameter nWidth und nHeight die Auflösung des Kamerabildes. Die Funktion gibt das "Video-Handle" zurück, welches für alles weitere benötigt wird. Mit "CapturePicture" wird das aktuelle Bild in die Zwischenablage kopiert und in eine Picturebox eingefügt. Und noch was: Wenn Sie mehr als nur eine Kamera am PC hängen haben, können Sie mit dem Parameter nCameraID die Nummer der Kamera angeben. Wenn Sie nur eine am Rechner hängen haben, müssen Sie dort eigentlich immer 0 angeben.

    Beispiel für einen Test:
    Starten Sie ein neues Projekt und setzen die Höhe der Form auf 4600 Twips. Nun fügen Sie einen CommandButton am untere Ende der Form ein. Den nennen Sie "cmdSave". Ziehen Sie noch eine PictureBox auf die Programmoberfläche. Deren Height-Eigenschaft setzen Sie auf 3600 und die Width-Eigenschaft auf 4800.

    Fügen Sie den obigen Code in ein Modul ein. Dann öffnen Sie per Doppelklick auf die Form das Code-Fenster. Dort fügen Sie folgenden Code ein:

    Option Explicit

    Dim Video_Handle as Long
    Private Sub Form_Load()
    Picture1.Visible = False
    Video_Handle = CreateCaptureWindow(Me.hwnd)
    End Sub
    Private Sub cmdSave_Click()
    CapturePicture Video_Handle, Picture1
    SavePicture Picture1.Picture, "C:\Test.bmp"
    End Sub
    Starten Sie das Programm. Wenn alles glatt läuft, müssten Sie Das Live-Bild der Video-Kamera auf der Form sehen und per Klick auf "Command1" das Bild unter "C:\Test.bmp" abspeichern können.



    Felix Kröger


    Wie sieht das denn mit einer normalen Kamera aus, die über eine CardBus Card (Notebookkarte) an den Rechneer angeschlossen ist? Funktioniert das auch oder geht das nur über USB-Cams?

    Oder -> Hat jemand einen Code, der das Videobild einer Kamera über SmartCard anzeigen kann? Wäre echt dankbar!!!!
    Hallo ONESIDE,

    der Code den du geschrieben hast funktioniert wunderbar, nur fehlt mir bissle die Peilung um ihn zu verstehen.

    Könntest du ihn vielleicht nochmal mit Argumenten schreiben??

    Wäre echt super nett.

    Also hoff das mir helfen kannst. THX

    Liebe Grüße Sasa
    Der Code ist von Kröger, nicht von Oneside. Außerdem ist der Beitrag schon etwas älter, sodass ich nicht glaube, dass Oneside dir antworten wird. Aber welche Zeile genau verstehst du denn nicht?
    Gruß, Agent Smith 8-)

    activeFlags = (lazy OR weary)

    Lemgo-Verschwörung | Mathematics | VB-Paradise in blau
    Also schon viel peil ich da nich.
    Die wo ich nich so versteh, mach ich Fett gedruckt


    Option Explicit

    ' benötigte Deklarationen
    Private Declare Function capCreateCaptureWindow Lib "avicap32.dll" _
    Alias "capCreateCaptureWindowA" ( _
    ByVal lpszWindowName As String, _
    ByVal dwStyle As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal hWndParent As Long, _
    ByVal nID As Long) As Long

    Private Const WS_CHILD = &H40000000
    Private Const WS_VISIBLE = &H10000000
    Private Const WM_USER = &H400
    Private Const WM_CAP_START = &H400
    Private Const WM_CAP_EDIT_COPY = (WM_CAP_START + 30)
    Private Const WM_CAP_DRIVER_CONNECT = (WM_CAP_START + 10)
    Private Const WM_CAP_SET_PREVIEWRATE = (WM_CAP_START + 52)
    Private Const WM_CAP_SET_OVERLAY = (WM_CAP_START + 51)
    Private Const WM_CAP_SET_PREVIEW = (WM_CAP_START + 50)
    Private Const WM_CAP_DRIVER_DISCONNECT = (WM_CAP_START + 11)


    Private Declare Function SendMessage Lib "user32" _
    Alias "SendMessageA" ( _
    ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    lParam As Any) As Long

    Private Preview_Handle As Long
    Public Function CreateCaptureWindow( _
    hWndParent As Long, _
    Optional x As Long = 0, _
    Optional y As Long = 0, _
    Optional nWidth As Long = 320, _
    Optional nHeight As Long = 240, _
    Optional nCameraID As Long = 0) As Long Preview_Handle = capCreateCaptureWindow("Video", _
    WS_CHILD + WS_VISIBLE, x, y, _
    nWidth, nHeight, hWndParent, 1)

    SendMessage Preview_Handle, WM_CAP_DRIVER_CONNECT, nCameraID, 0
    SendMessage Preview_Handle, WM_CAP_SET_PREVIEWRATE, 30, 0
    SendMessage Preview_Handle, WM_CAP_SET_OVERLAY, 1, 0
    SendMessage Preview_Handle, WM_CAP_SET_PREVIEW, 1, 0

    CreateCaptureWindow = Preview_Handle
    End Function
    Public Sub CapturePicture(nCaptureHandle As Long, _
    picCapture As PictureBox)

    Clipboard.Clear
    SendMessage nCaptureHandle, WM_CAP_EDIT_COPY, 0, 0
    picCapture.Picture = Clipboard.GetData
    End Sub
    Public Sub Disconnect(nCaptureHandle As Long, _
    Optional nCameraID = 0)

    SendMessage nCaptureHandle, WM_CAP_DRIVER_DISCONNECT, _
    nCameraID, 0

    End Sub



    Option Explicit

    Dim Video_Handle as Long
    Private Sub Form_Load()
    Picture1.Visible = False
    Video_Handle = CreateCaptureWindow(Me.hwnd)
    End Sub
    Private Sub cmdSave_Click()
    CapturePicture Video_Handle, Picture1
    SavePicture Picture1.Picture, "C:\Test.bmp"
    End Sub




    Also wie gesagt, ist schon ne Menge aber des is deswegen,weil ich noch nich so viel mit externen Geräten in VB gearbeitet hab.
    Is bissle neu für mich.


    Also des Beste wärs, wenn du für jede Zeile en Argument schreiben könntest was einleuchtend klingt. :D

    Das wäre natürlich genial :P

    Also liebe grüße Sasa