WEbcam in VB.NET ansprechen

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von bastler.

    WEbcam in VB.NET ansprechen

    Hallo,
    ich versuche meine Webcam in VB.NET anzusprechen und das Bild in einer Picturebox darzustellen. Wenn ich meinen PC neu starte und den nachfolgenden Programmcode ausführe, dann erhalte ich manchmal ein Bild, aber die meiste zeit hab ich nur einen schwarze picturebox und ich bekomm die meldung das ich eine Videoquelle auswählen soll. was ich dann mache, aber es tut trotzdem nicht. kann mir jemand helfen?

    VB.NET-Quellcode

    1. Public Class Form1
    2. Const WM_CAP As Short = &H400S
    3. Const WM_CAP_DRIVER_CONNECT As Integer = WM_CAP + 10
    4. Const WM_CAP_DRIVER_DISCONNECT As Integer = WM_CAP + 11
    5. Const WM_CAP_EDIT_COPY As Integer = WM_CAP + 30
    6. Const WM_CAP_SET_PREVIEW As Integer = WM_CAP + 50
    7. Const WM_CAP_SET_PREVIEWRATE As Integer = WM_CAP + 52
    8. Const WM_CAP_SET_SCALE As Integer = WM_CAP + 53
    9. Const WS_CHILD As Integer = &H40000000
    10. Const WS_VISIBLE As Integer = &H10000000
    11. Const SWP_NOMOVE As Short = &H2S
    12. Const SWP_NOSIZE As Short = 1
    13. Const SWP_NOZORDER As Short = &H4S
    14. Const HWND_BOTTOM As Short = 1
    15. Dim iDevice As Integer = 0
    16. Dim hHwnd As Integer
    17. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
    18. 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
    19. Declare Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
    20. 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
    21. 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
    22. Private Sub LoadDeviceList()
    23. Dim strName As String = Space(100)
    24. Dim strVer As String = Space(100)
    25. Dim bReturn As Boolean
    26. Dim x As Integer = 0
    27. Do
    28. bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
    29. If bReturn Then lstDevices.Items.Add(strName.Trim)
    30. x += 1
    31. Loop Until bReturn = False
    32. End Sub
    33. Private Sub OpenPreviewWindow()
    34. Dim iHeight As Integer = picCapture.Height
    35. Dim iWidth As Integer = picCapture.Width
    36. hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, picCapture.Handle.ToInt32, 0)
    37. If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
    38. SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
    39. SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
    40. SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
    41. SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
    42. btnSave.Enabled = True
    43. btnStop.Enabled = True
    44. btnStart.Enabled = False
    45. Else
    46. DestroyWindow(hHwnd)
    47. btnSave.Enabled = False
    48. End If
    49. End Sub
    50. Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    51. Dim data As IDataObject
    52. Dim bmap As Image
    53. SendMessage(hHwnd, WM_CAP_EDIT_COPY, 0, 0)
    54. data = Clipboard.GetDataObject()
    55. If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
    56. bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
    57. picCapture.Image = bmap
    58. ClosePreviewWindow()
    59. btnSave.Enabled = False
    60. btnStop.Enabled = False
    61. btnStart.Enabled = True
    62. If sfdImage.ShowDialog = DialogResult.OK Then
    63. bmap.Save(sfdImage.FileName, Imaging.ImageFormat.Bmp)
    64. End If
    65. End If
    66. End Sub
    67. Private Sub ClosePreviewWindow()
    68. SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
    69. DestroyWindow(hHwnd)
    70. End Sub
    71. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    72. LoadDeviceList()
    73. End Sub
    74. Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
    75. OpenPreviewWindow()
    76. btnStart.Enabled = False
    77. btnStop.Enabled = True
    78. End Sub
    79. Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
    80. ClosePreviewWindow()
    81. btnStart.Enabled = True
    82. btnStop.Enabled = False
    83. End Sub
    84. End Class


    *Topic verschoben*

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Marcus Gräfe“ ()