vb.net摄影机pinvoke错误
这个问题以前可能有人问过,但我刚刚开始使用VB.Net,并得到了这个应用程序来修复它,它使用pc/平板电脑的网络摄像头,但我无法找出pinvoke错误 这是我的密码:vb.net摄影机pinvoke错误,vb.net,pinvoke,webcam,Vb.net,Pinvoke,Webcam,这个问题以前可能有人问过,但我刚刚开始使用VB.Net,并得到了这个应用程序来修复它,它使用pc/平板电脑的网络摄像头,但我无法找出pinvoke错误 这是我的密码: Imports System.IO Public Class frm CaptureWebCam Const CAP As Short = &H400S Const CAP_DRIVER_CONNECT As Integer = CAP + 10 Const CAP_DRIVER_DISCON
Imports System.IO
Public Class frm
CaptureWebCam
Const CAP As Short = &H400S
Const CAP_DRIVER_CONNECT As Integer = CAP + 10
Const CAP_DRIVER_DISCONNECT As Integer = CAP + 11
Const CAP_EDIT_COPY As Integer = CAP + 30
Const CAP_SET_PREVIEW As Integer = CAP + 50
Const CAP_SET_PREVIEWRATE As Integer = CAP + 52
Const CAP_SET_SCALE As Integer = CAP + 53
Const WS_CHILD As Integer = &H40000000
Const WS_VISIBLE As Integer = &H10000000
Const SWP_NOMOVE As Short = &H2S
Const SWP_NOSIZE As Short = 1
Const SWP_NOZORDER As Short = &H4S
Const HWND_BOTTOM As Short = 1
Dim iDevice As Integer = 0 ' Normal device ID
Dim hHwnd As Integer ' Handle value to preview window
Public image_base64String As String
' Declare function from AVI capture DLL.
Declare Auto Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer
Declare Auto 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 Auto Function DestroyWindow Lib "user32" (ByVal hndw As Integer) As Boolean
Declare Auto 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
Private Sub OpenForm()
Dim iHeight As Integer = picCapture.Height
Dim iWidth As Integer = picCapture.Width
' Open Preview window in picturebox .
' Create a child window with capCreateCaptureWindowA so you can display it in a picturebox.
hHwnd = capCreateCaptureWindowA(iDevice, WS_VISIBLE Or WS_CHILD, 0, 0, 600, 480, picCapture.Handle, IntPtr.Zero)
' Connect to device
If SendMessage(hHwnd, CAP_DRIVER_CONNECT, iDevice, IntPtr.Zero) Then
' Set the preview scale
SendMessage(hHwnd, CAP_SET_SCALE, True, IntPtr.Zero)
' Set the preview rate in milliseconds
SendMessage(hHwnd, CAP_SET_PREVIEWRATE, 66, IntPtr.Zero)
' Start previewing the image from the camera
SendMessage(hHwnd, CAP_SET_PREVIEW, True, IntPtr.Zero)
' Resize window to fit in picturebox
SetWindowPos(hHwnd, HWND_BOTTOM, 0, 0, picCapture.Width, picCapture.Height, SWP_NOMOVE Or SWP_NOZORDER)
Else
' Error connecting to device close window
DestroyWindow(hHwnd)
End If
End Sub
Private Function btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapture.Click
Dim data As IDataObject
Dim bmap As Image
' Copy image to clipboard
SendMessage(hHwnd, CAP_EDIT_COPY, 0, 0)
' Get image from clipboard and convert it to a bitmap
data = Clipboard.GetDataObject()
If data.GetDataPresent(GetType(System.Drawing.Bitmap)) Then
bmap = CType(data.GetData(GetType(System.Drawing.Bitmap)), Image)
picCapture.Image = bmap
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "Jpeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif"
saveFileDialog1.Title = "Save an Image File"
saveFileDialog1.FileName = "Image001"
If saveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
' If the file name is not an empty string open it for saving.
If saveFileDialog1.FileName <> "" Then
' Saves the Image via a FileStream created by the OpenFile method.
Dim fs As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)
picCapture.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
fs.Close()
End If
End If
End If
End Function
Public Function getImage() As String
Dim bmap As Image
Dim ms As New MemoryStream
bmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytes() As Byte = ms.ToArray
' Dim image_base64String As String = Convert.ToBase64String(bytes)
image_base64String = Convert.ToBase64String(bytes)
'MsgBox(image_base64String)
Return image_base64String
End Function
Private Sub frmcap_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
' Disconnect from device
SendMessage(hHwnd, CAP_DRIVER_DISCONNECT, iDevice, 0)
' close window
DestroyWindow(hHwnd)
End Sub
Private Sub frmCaptureWebCam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
OpenForm()
End Sub
End Class
Imports System.IO
公共类frm
捕获网络摄像机
Const CAP As Short=&H400S
Const CAP_DRIVER_CONNECT As Integer=CAP+10
常量CAP\u驱动器\u断开连接为整数=CAP+11
常量CAP\u编辑\u复制为整数=CAP+30
常量上限设置预览为整数=上限+50
常量CAP\u SET\u PREVIEWRATE为整数=CAP+52
常量上限设置为整数=上限+53
Const WS_CHILD作为整数=&H40000000
常量WS_显示为整数=&H10000000
Const SWP_NOMOVE As Short=&H2S
Const SWP_NOSIZE As Short=1
常量SWP_NOZORDER为Short=&H4S
Const HWND_底部短=1
Dim iDevice作为整数=0'正常设备ID
将hHwnd设置为整数“句柄值以预览窗口
公共映像\u base64字符串作为字符串
'从AVI捕获DLL声明函数。
将自动函数SendMessage Lib“user32”别名“SendMessageA”(ByVal hwnd为整数,ByVal wMsg为整数,ByVal wParam为整数,ByVal lParam为对象)声明为整数
将自动函数SetWindowPos Lib“user32”别名“SetWindowPos”(ByVal hwnd为整数,ByVal hwninsertafter为整数,ByVal x为整数,ByVal y为整数,ByVal cx为整数,ByVal cy为整数,ByVal wFlags为整数)声明为整数
将自动函数DestroyWindow库“user32”(ByVal hndw为整数)声明为布尔值
将自动函数capCreateCaptureWindowA Lib“avicap32.dll”(ByVal lpszWindowName为字符串,ByVal dwStyle为整数,ByVal x为整数,ByVal y为整数,ByVal nWidth为整数,ByVal nHeight为短,ByVal hwndpent为整数,ByVal nID为整数)声明为整数
私有子OpenForm()
尺寸iHeight为整数=picCapture.Height
Dim iWidth作为整数=picCapture.Width
'在picturebox中打开预览窗口。
'使用capCreateCaptureWindowA创建子窗口,以便可以在图片框中显示它。
hHwnd=capCreateCaptureWindowA(iDevice、WS_可见或WS_子对象、0、0、600、480、picCapture.Handle、IntPtr.Zero)
'连接到设备
如果发送消息(hHwnd、CAP\u驱动程序\u CONNECT、iDevice、IntPtr.Zero),则
'设置预览比例
SendMessage(hHwnd、CAP\U SET\U SCALE、True、IntPtr.Zero)
'以毫秒为单位设置预览速率
发送消息(hHwnd,CAP\U SET\U PREVIEWRATE,66,IntPtr.Zero)
'开始从相机预览图像
SendMessage(hHwnd,CAP\u SET\u PREVIEW,True,IntPtr.Zero)
'调整窗口大小以适合picturebox
设置窗口位置(hHwnd、HWND_底部、0、0、picCapture.Width、picCapture.Height、SWP_NOMOVE或SWP_NOZORDER)
其他的
'连接到设备关闭窗口时出错
销毁窗口(hHwnd)
如果结束
端接头
私有函数btnSave_Click(ByVal发送方作为System.Object,ByVal e作为System.EventArgs)处理btnCapture。单击
作为IDataObject的Dim数据
作为图像的暗淡bmap
'将图像复制到剪贴板
发送消息(hHwnd,CAP\u编辑\u复制,0,0)
'从剪贴板获取图像并将其转换为位图
data=Clipboard.GetDataObject()
如果data.GetDataPresent(GetType(System.Drawing.Bitmap)),则
bmap=CType(data.GetData(GetType(System.Drawing.Bitmap)),Image)
picCapture.Image=bmap
将saveFileDialog1设置为新的SaveFileDialog()
saveFileDialog1.Filter=“Jpeg图像|*.jpg |位图图像|*.bmp | Gif图像|*.Gif”
saveFileDialog1.Title=“保存图像文件”
saveFileDialog1.FileName=“Image001”
如果saveFileDialog1.ShowDialog()=Windows.Forms.DialogResult.OK,则
'如果文件名不是空字符串,请打开它进行保存。
如果是saveFileDialog1.FileName“”,则
'通过OpenFile方法创建的文件流保存图像。
Dim fs As System.IO.FileStream=CType(saveFileDialog1.OpenFile(),System.IO.FileStream)
picCapture.Image.Save(fs,System.Drawing.Imaging.ImageFormat.Jpeg)
财政司司长(关闭)
如果结束
如果结束
如果结束
端函数
作为字符串的公共函数getImage()
作为图像的暗淡bmap
将ms变暗为新内存流
bmap.Save(ms、System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytes()作为Byte=ms.ToArray
'Dim image_base64String As String=Convert.ToBase64String(字节)
image\u base64String=Convert.ToBase64String(字节)
'MsgBox(图像\u base64String)
返回图像\u base64String
端函数
私有子frmcap_Leave(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Leave
'断开与设备的连接
SendMessage(hHwnd,盖驱动器断开,iDevice,0)
“关上窗户
销毁窗口(hHwnd)
端接头
私有子frmCaptureWebCam_Load(发送者作为对象,e作为事件参数)处理MyBase.Load
OpenForm()
端接头
末级
您会注意到,我还试图通过image_base64变量返回图像
当我遇到以下错误时,有人能帮我处理代码吗:
检测到PinvokesTack不平衡
消息:调用PInvoke函数
'GCOS3\移动\主机\应用程序!GCOS3\移动\主机\应用程序。frmCaptureWebCam::SendMessage'
使堆栈不平衡。这可能是因为管理的PInvoke
签名与非托管目标签名不匹配。检查一下
PInvoke签名匹配的调用约定和参数
目标非托管签名
SendMessage
根据的声明应该是这样的
声明自动函数SendMessage Lib“user32.dll”(ByVal hWnd为IntPtr,ByVal msg为Integer,
Imports Emgu.CV
Private imagecapture As Capture
Private imageCaptureReady As Boolean = False
Public b64 As String
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles tmrUpdateImage.Tick
tmrUpdateImage.Enabled = False
If imageCaptureReady Then
pbWebCamStream.Visible = True
lblConnecting.Visible = False
btnCapture.Enabled = True
pbWebCamStream.Image = imagecapture.QueryFrame.Bitmap
tmrUpdateImage.Enabled = True
Else
MessageBox.Show("Error connecting to camera.", "Error conecting to camera.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
End If
End Sub
Public Function btnCapture_Click(sender As Object, e As EventArgs) Handles btnCapture.Click
tmrUpdateImage.Enabled = False
pbWebCamStream.Visible = True
lblConnecting.Visible = False
btnCapture.Enabled = True
pbWebCamStream.Image = imagecapture.QueryFrame.Bitmap
Dim bmap As Image
bmap = imagecapture.QueryFrame.Bitmap
Dim ms As New MemoryStream
bmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Dim bytes() As Byte = ms.ToArray
Dim image_base64String As String = Convert.ToBase64String(bytes)
image_base64String = Convert.ToBase64String(bytes)
b64 = image_base64String
imagecapture.Dispose()
Me.Close()
'MsgBox(image_base64String)
Return image_base64String
End Function
Private Sub frmEmguCapture_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tmrLoad.Enabled = True
End Sub
Private Sub tmrLoad_Tick(sender As Object, e As EventArgs) Handles tmrLoad.Tick
tmrLoad.Enabled = False
Try
imagecapture = New Capture
imageCaptureReady = True
tmrUpdateImage.Enabled = True
Catch ex As Exception
MessageBox.Show("Error connecting to camera.", "Error conecting to camera.", MessageBoxButtons.OK, MessageBoxIcon.Error)
Me.Close()
End Try
End Sub