我正在尝试使用 Visual Basic 开发一个 Windows 应用程序来从网络摄像头捕获照片。我正在使用 EmguCV 包装库。下面是我的代码,它导致“重载解析失败,因为无法使用这些参数调用可访问的‘New’”。谁能建议如何解决这个错误?
`
Imports Emgu.CV
Imports Emgu.CV.Structure
Imports Emgu.CV.CvEnum
Imports System.Drawing
Imports System.Globalization
Public Class WebCamForm
Private capture As VideoCapture
Public ReadOnly Property InstalledUICulture As CultureInfo
Private Sub PhotoCaptureForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Initialize the Capture object for webcam access
capture = New VideoCapture()
End Sub
Private Sub Button_CapturePhoto_Click(sender As Object, e As EventArgs) Handles Button_CapturePhoto.Click
' Capture a frame from the webcam
Dim currentFrame As Mat = capture.QueryFrame()
If currentFrame IsNot Nothing Then
' Convert the Mat object to an Image(Of Bgr, Byte) object
Dim imageFrame As Image(Of Bgr, Byte) = currentFrame.ToImage(Of Bgr, Byte)()
' Convert the Image(Of Bgr, Byte) object to a Bitmap object
Dim bitmapFrame As Bitmap = New Bitmap(imageFrame)
' Display the captured image in the PictureBox
PictureBox1.Image = bitmapFrame
' Save the captured image to a file
Dim fileName As String = "captured_photo.jpg"
bitmapFrame.Save(fileName)
End If
End Sub
Private Sub PhotoCaptureForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
' Release the resources associated with the Capture object
If capture IsNot Nothing Then
capture.Dispose()
End If
End Sub
End Class
`
我收到的错误消息:
Severity Code Description Project File Line Suppression State Error (active) BC30518 Overload resolution failed because no accessible 'New' can be called with these arguments: 'Public Overloads Sub New(filename As String)': Value of type 'Image(Of Bgr, Byte)' cannot be converted to 'String'. 'Public Overloads Sub New(stream As Stream)': Value of type 'Image(Of Bgr, Byte)' cannot be converted to 'Stream'. 'Public Overloads Sub New(original As Image)': Value of type 'Image(Of Bgr, Byte)' cannot be converted to 'Image'. IFAB_Control_Center C:\Users\user\source\repos\IFAB\WebCamForm.vb 25
以下展示了如何在 VB.NET 中使用 Emgu.CV NuGet 包 (v4.9.0.5494) 和计算机上的网络摄像头捕获图像。
尝试以下操作:
创建 Windows 窗体应用程序 (.NET Framework)
将您的项目更改为 x64(或 x86),请参阅 Emgu CV - 针对 .NET Framework
下载安装以下 NuGet 包:
添加以下导入
Imports Emgu.CV
Imports Emgu.CV.BitmapExtension
Imports System.IO
选项 1(捕获图像):
Private Function CaptureImage() As Byte()
Dim imageBytes As Byte() = Nothing
'create new instance
Using frame As Mat = New Mat()
'create new instance
Using capture As VideoCapture = New VideoCapture()
'capture video frame
capture.Read(frame)
End Using
'save as bitmap
Using bmp As Bitmap = frame.ToBitmap()
Using ms As MemoryStream = New MemoryStream()
bmp.Save(ms, Imaging.ImageFormat.Bmp)
'convert to Byte()
imageBytes = ms.ToArray()
End Using
End Using
End Using
Return imageBytes
End Function
选项 2(捕获图像):
Private Function CaptureImage() As Byte()
Dim imageBytes As Byte() = Nothing
'create new instance
Using capture As VideoCapture = New VideoCapture()
'capture video frame
Using frame As Mat = capture.QueryFrame()
'save as bitmap
Using bmp As Bitmap = frame.ToBitmap()
Using ms As MemoryStream = New MemoryStream()
bmp.Save(ms, Imaging.ImageFormat.Bmp)
'convert to Byte()
imageBytes = ms.ToArray()
End Using
End Using
End Using
End Using
Return imageBytes
End Function
用法1:
在窗体中添加一个PictureBox控件(名称:PictureBox1)
Dim imageBytes As Byte() = CaptureImage()
Using ms As MemoryStream = New MemoryStream(imageBytes)
PictureBox1.Image = Image.FromStream(ms)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
End Using
用法2:
Dim imageBytes As Byte() = CaptureImage()
Using sfd As SaveFileDialog = New SaveFileDialog()
sfd.Filter = "Bitmap File (*.bmp)|*.bmp"
sfd.FileName = "Test.bmp"
If sfd.ShowDialog() = DialogResult.OK Then
System.IO.File.WriteAllBytes(sfd.FileName, imageBytes)
System.Diagnostics.Debug.WriteLine($"Saved to '{sfd.FileName}'")
End If
End Using
资源: