VB.NET使用EmguCV捕获摄像头照片遇重载解析错误求助
问题解决:EmguCV转Bitmap时的重载解析错误
错误原因
报错行是代码第25行:Dim bitmapFrame As Bitmap = New Bitmap(imageFrame)。EmguCV的Image(Of Bgr, Byte)属于自定义封装类型,并非.NET原生的System.Drawing.Image或可直接适配Bitmap构造函数的类型,因此编译器找不到匹配的重载方法,触发BC30518错误。
解决方法
直接使用EmguCV的Image(Of TColor, TDepth)对象自带的Bitmap属性,就能直接获取对应的.NET Bitmap实例,无需手动调用Bitmap构造函数:
修改这一行代码:
Dim bitmapFrame As Bitmap = imageFrame.Bitmap
修正后的完整代码
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 = imageFrame.Bitmap ' 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
额外说明
EmguCV的Image类已经封装了与.NET绘图对象的转换接口,直接使用Bitmap属性是最简洁且安全的方式,既避免了类型不匹配的问题,也能保证资源正确处理。
内容的提问来源于stack exchange,提问作者ASTRA
相关产品推荐
相关产品推荐

