1

I have a question about drawing a rectangle on VB picturebox when capturing webcam view,when I run this code,the picturebox can shows what webcam capture,but it doesn't show any red rectangle on picturebox, How to solve this problem??

My code is below:

Const WM_CAP As Short = &H400S
Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP + 41
Const WM_CAP_DLG_VIDEOSOURCE As Integer = WM_CAP + 42
Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
Const WS_CHILD As Integer = &H40000000
Const WS_VISIBLE As Integer = &H10000000
Const SWP_NOMOVE As Short = &H25
Const SWP_NOSIZE As Short = 1
Const SWP_NOZORDER As Short = &H4S
Const HWND_BOTTOM As Short = 1
Dim iDevice As Integer = 0
Dim hHwnd As Integer
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByRef RECT As IntPtr) As IntPtr
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
Declare Function DestroyWindow Lib "user32" (ByVal hwnd As Integer) As Boolean
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
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
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
    Me.OpenPreviewWindow()
    Me.Timer1.Start()
End Sub

Private Sub OpenPreviewWindow()
    Me.hHwnd = capCreateCaptureWindowA(iDevice.ToString, WS_VISIBLE Or WS_CHILD, 0, 0, 320, 240, Me.pcbCapture.Handle.ToInt32, 0)
    If CType(SendMessage(CType(hHwnd, IntPtr), WM_CAP_DRIVER_CONNECT, CType(iDevice, IntPtr), CType(0, IntPtr)), IntPtr) = CType(1, IntPtr) Then
        SendMessage(CType(hHwnd, IntPtr), WM_CAP_SET_SCALE, CType(True, IntPtr), CType(0, IntPtr))
        SendMessage(CType(hHwnd, IntPtr), WM_CAP_SET_PREVIEWRATE, CType(66, IntPtr), CType(0, IntPtr))
        SendMessage(CType(hHwnd, IntPtr), WM_CAP_SET_PREVIEW, CType(True, IntPtr), CType(0, IntPtr))
        SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, Me.pcbCapture.Width, Me.pcbCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
    Else
        DestroyWindow(hHwnd)
    End If
End Sub

Dim data As IDataObject
Dim bmp As Image
Dim img As Image
Dim g As Graphics
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    SendMessage(CType(hHwnd, IntPtr), WM_CAP_EDIT_COPY, CType(0, IntPtr), CType(0, IntPtr))
    data = Clipboard.GetDataObject
    If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
        bmp = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
        img = bmp
        If img IsNot Nothing Then
            Dim p As New Pen(Brushes.Red, 1)
            g = Graphics.FromImage(img)
            g.DrawRectangle(p, CInt(img.Width / 2 - 55), CInt(img.Height / 2 + 25), 110, 50)
            Me.pcbCapture.Image = img
        End If
    End If
End Sub
Arthur
  • 11
  • 2

0 Answers0