在C#中使用网络摄像头 [英] Using the Webcam with C#

查看:89
本文介绍了在C#中使用网络摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我已经在VB.Net中编程了一段时间,并决定尝试使用Visual C#来学习新内容。 目前,我正在努力将我的一个VB应用程序转换为C#。该程序具有一项功能,允许用户通过avicap32.dll和user32.dll使用
网络摄像头捕获图像。 它在VB.Net中运行良好,但当我把它放入Telerik的代码转换器并将结果放入C#类时,它给我带来了惊人的27个错误! 这是原始代码。 有人可以告诉我
如何正确转换它所以它适用于C#? 提前致谢! 


Jason


代码:

 Imports System.Runtime.InteropServices 
Imports System.Data.SQLite

Module Camera
Public FileName As String
Public Const WM_USER As Integer = 1024
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_NOZORDE R As Short =& H4S
Public Const HWND_BOTTOM As Short = 1
Public Const WM_CAP_START As Long = WM_USER

Public Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP_START + 41
公共Const WM_CAP_DLG_VIDEOSOURCE As Integer = WM_CAP_START + 42
Public Const WM_CAP_DLG_VIDEODISPLAY As Integer = WM_CAP_START + 43
Public Const WM_CAP_GET_VIDEOFORMAT As Integer = WM_CAP_START + 44
Public Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_CAP_START + 45
Public Const WM_CAP_DLG_VIDEOCOMPRESSION As Integer = WM_CAP_START + 46

Public iDevice As Integer = 0
Public hHwnd As Integer

Public Declare Function SendMessage Lib" user32"别名"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"别名"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

Public Sub LoadDeviceList( )
Dim CurForm As Form = Form.ActiveForm
Dim cmb As ComboBox = TryCast(CurForm.ActiveControl,ComboBox)
If Not IsNothing(cmb)Then
Dim strName As String =空格(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
Do
bReturn = capGetDriverDescriptionA(x, strName,100,strVer,100)
如果bReturn则cmb.Items.Add(strName.Trim)
x + = 1
Loop Until bReturn = False
End if
End Sub
Public Sub OpenPreviewWindow()
Dim CurForm As Form = Form.ActiveForm
Dim pbx As PictureBox = TryCast(CurForm.ActiveControl,Pict ureBox)
如果不是IsNothing(pbx)那么
Dim iHeight As Integer = pbx.Height
Dim iWidth As Integer = pbx.Width
hHwnd = capCreateCaptureWindowA(iDevice,WS_VISIBLE或WS_CHILD ,0,0,640,480,pbx.Handle.ToInt32,0)
如果SendMessage(hHwnd,WM_CAP_DRIVER_CONNECT,iDevice,0)那么
SendMessage(hHwnd,WM_CAP_SET_SCALE,True,0)
SendMessage(hHwnd,WM_CAP_SET_PREVIEWRATE,66,0)
SendMessage(hHwnd,WM_CAP_SET_PREVIEW,True,0)
SetWindowPos(hHwnd,HWND_BOTTOM,0,0,iWidth,iHeight,SWP_NOMOVE或SWP_NOZORDER)
Else
DestroyWindow(hHwnd)
End if
End if
End Sub
Public Sub ClosePreviewWindow()
SendMessage(hHwnd,WM_CAP_DRIVER_DISCONNECT,iDevice,0 )
DestroyWindow(hHwnd)
End Sub
公共函数DeviceCount()As Integer
Dim strName As String = Space(100)
Dim strVer As String = Space(100)
Dim bReturn As Boolean
Dim x As Integer = 0
Dim Devices作为新列表(字符串)
Do
bReturn = capGetDriverDescriptionA(x,strName,100,strVer,100)
如果bReturn然后Devices.Add(strName.Trim)
循环直到bReturn = False
返回Devices.Count
结束函数
结束模块



解决方案

你可以使用这个工具WebCam_Capture.dll然后在图片框中显示this.WebCamCapture.CaptureHeight = this.pictureBox1.Height; this.WebCamCapture.CaptureWidth = this.pictureBox1.Width; this.WebCamCapture.TimeToCapture_milliseconds = 100; this.WebCamCapture.Start(0);

Hi all,

I've been programming in VB.Net for a while and decided to give Visual C# a try to learn something new.  Currently, I'm working on converting one of my VB applications to C#. The program has a feature that allows the user to capture images using the webcam via avicap32.dll and user32.dll.  It works fine in VB.Net, but when I put it into Telerik's code converter and put the result into a C# class, it threw a whopping 27 errors at me!  Here's the original code.  Can someone please tell me how to properly convert it so it works in C#?  Thanks in advance! 

Jason

The Code:

Imports System.Runtime.InteropServices
Imports System.Data.SQLite

Module Camera
    Public FileName As String
    Public Const WM_USER As Integer = 1024
    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 Const WM_CAP_START As Long = WM_USER

    Public Const WM_CAP_DLG_VIDEOFORMAT As Integer = WM_CAP_START + 41
    Public Const WM_CAP_DLG_VIDEOSOURCE As Integer = WM_CAP_START + 42
    Public Const WM_CAP_DLG_VIDEODISPLAY As Integer = WM_CAP_START + 43
    Public Const WM_CAP_GET_VIDEOFORMAT As Integer = WM_CAP_START + 44
    Public Const WM_CAP_SET_VIDEOFORMAT As Integer = WM_CAP_START + 45
    Public Const WM_CAP_DLG_VIDEOCOMPRESSION As Integer = WM_CAP_START + 46

    Public iDevice As Integer = 0
    Public hHwnd As Integer

    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

    Public Sub LoadDeviceList()
        Dim CurForm As Form = Form.ActiveForm
        Dim cmb As ComboBox = TryCast(CurForm.ActiveControl, ComboBox)
        If Not IsNothing(cmb) Then
            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 cmb.Items.Add(strName.Trim)
                x += 1
            Loop Until bReturn = False
        End If
    End Sub
    Public Sub OpenPreviewWindow()
        Dim CurForm As Form = Form.ActiveForm
        Dim pbx As PictureBox = TryCast(CurForm.ActiveControl, PictureBox)
        If Not IsNothing(pbx) Then
            Dim iHeight As Integer = pbx.Height
            Dim iWidth As Integer = pbx.Width
            hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 640, 480, pbx.Handle.ToInt32, 0)
            If SendMessage(hHwnd, WM_CAP_DRIVER_CONNECT, iDevice, 0) Then
                SendMessage(hHwnd, WM_CAP_SET_SCALE, True, 0)
                SendMessage(hHwnd, WM_CAP_SET_PREVIEWRATE, 66, 0)
                SendMessage(hHwnd, WM_CAP_SET_PREVIEW, True, 0)
                SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, iWidth, iHeight, SWP_NOMOVE Or SWP_NOZORDER)
            Else
                DestroyWindow(hHwnd)
            End If
        End If
    End Sub
    Public Sub ClosePreviewWindow()
        SendMessage(hHwnd, WM_CAP_DRIVER_DISCONNECT, iDevice, 0)
        DestroyWindow(hHwnd)
    End Sub
    Public Function DeviceCount() As Integer
        Dim strName As String = Space(100)
        Dim strVer As String = Space(100)
        Dim bReturn As Boolean
        Dim x As Integer = 0
        Dim Devices As New List(Of String)
        Do
            bReturn = capGetDriverDescriptionA(x, strName, 100, strVer, 100)
            If bReturn Then Devices.Add(strName.Trim)
        Loop Until bReturn = False
        Return Devices.Count
    End Function
End Module

解决方案

you can to using this tool WebCam_Capture.dll and then display in picturebox this.WebCamCapture.CaptureHeight = this.pictureBox1.Height; this.WebCamCapture.CaptureWidth = this.pictureBox1.Width; this.WebCamCapture.TimeToCapture_milliseconds = 100; this.WebCamCapture.Start(0);


这篇关于在C#中使用网络摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆