ce programme contient un code qui permet l'affichage d'un prévisualisation de vidéo avec le vb.net 2010
Source / Exemple :
'Module modWebcam''''''''''''''''''
Option Explicit On
Option Strict Off
Imports System
Imports System.Runtime
Imports System.Runtime.InteropServices
Module modWebcam
Public Const WM_CAP As Short = &H400S
Public Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
Public Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
Public Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
Public Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
Public Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
Public Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
Public Const WS_CHILD As Integer = &H40000000
Public Const WS_VISIBLE As Integer = &H10000000
Public Const SWP_NOMOVE As Short = &H2S
Public Const SWP_NOSIZE As Short = 1
Public Const SWP_NOZORDER As Short = &H4S
Public Const HWND_BOTTOM As Short = 1
Public iDevice As Integer = 0 ' Current device ID
Public hHwnd As Integer ' Handle to preview window
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, _
<MarshalAs(UnmanagedType.AsAny)> ByVal lParam As Object) As Integer
Public Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, _
ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
Public Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Public 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
Public 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
End Module
'Fin Module modWebcam''''''''''''''''''
'Form1
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadDeviceList()
End Sub
Private Sub LoadDeviceList()
On Error Resume Next
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
Do
bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
If bReturn Then lst1.Items.Add(strName.Trim)
x += 1
Application.DoEvents()
Loop Until bReturn = False
End Sub
'This function is used to start capture frame.
'Open View
Private Sub OpenPreviewWindow()
On Error Resume Next
Dim iHeight As Integer = pview.Height
Dim iWidth As Integer = pview.Width
'
' Open Preview window in picturebox
'
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, _
480, pview.Handle.ToInt32, 0)
'
' Connect to device
'
If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
'
'Set the preview scale
'
SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
'
'Set the preview rate in milliseconds
'
SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
'
'Start previewing the image from the camera
'
SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
'
' Resize window to fit in picturebox
'
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, pview.Width, pview.Height, _
SWP_NOMOVE Or SWP_NOZORDER)
Else
'
' Error connecting to device close window
'
DestroyWindow(hHwnd)
End If
End Sub
'This function is used to close Capture Function.
Private Sub ClosePreviewWindow()
On Error Resume Next
'
' Disconnect from device
'
SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
'
' close window
'
DestroyWindow(hHwnd)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Lst1.Items.Count = 0 Then
MsgBox("Il n'y'a aucune caméra installé", vbCritical, "Pas de caméra")
End If
If Lst1.SelectedIndex < 0 Then
MsgBox("il faut sélection une ligne dans la liste!", vbInformation, "Erreur caméra")
Else
OpenPreviewWindow()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ClosePreviewWindow()
End Sub
End Class
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.