Mat 到 Bitmap 转换器 - 参数异常
Mat to Bitmap converter - Argument Exception
我在 VB Net Forms 应用程序中使用 OpenCVSharp3。我有这个与 C# 一起工作,但现在正在尝试转换为 VB Net。我想将网络摄像头提要显示到图片框。
''' <summary>
''' Feed from camera
''' </summary>
''' <remarks></remarks>
Private _capture As VideoCapture
''' <summary>
''' Frame from camera
''' </summary>
''' <remarks></remarks>
Private _frame As Mat
''' <summary>
''' Image
''' </summary>
''' <remarks></remarks>
Private _image As Bitmap
Private Sub CaptureCameraCallback()
Try
_isCameraRunning = True
_frame = New Mat()
_capture = New VideoCapture()
_capture.Open(_cameraDevice)
_capture.Read(_frame)
If _frame IsNot Nothing Then
While _isCameraRunning
_image = BitmapConverter.ToBitmap(_frame)
captureFrame.Image = _image
_image = Nothing
End While
End If
Catch ex As Exception
End Try
End Sub
Private Sub CaptureCamera()
_camera = New Thread(AddressOf CaptureCameraCallback)
_camera.Start()
End Sub
我有上面的代码可以将网络摄像头提要显示到图片框。但是当我 运行 它时,我得到以下异常:
A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
异常发生在线路:
_image = BitmapConverter.ToBitmap(_frame)
我想我需要在某个地方进行处理,但不确定这是否是这里的问题。
正在运行的 C# 代码 -
// Declare required methods
private void CaptureCamera()
{
camera = new Thread(new ThreadStart(CaptureCameraCallback));
camera.Start();
}
private void CaptureCameraCallback()
{
try
{
frame = new Mat();
capture = new VideoCapture();
capture.Open(CameraDevice);
while (isCameraRunning)
{
capture.Read(frame);
image = BitmapConverter.ToBitmap(frame);
captureFrame.Image = image;
image = null;
}
}
catch (Exception)
{
MessageBox.Show("No camera found on device", "Start Stream",
MessageBoxButtons.OK, MessageBoxIcon.Error);
if (streamBtn.InvokeRequired)
{
streamBtn.Invoke(new MethodInvoker(delegate { streamBtn.Text = "Start"; }));
}
}
}
解决方案
''' <summary>
''' Get feed from camera
''' </summary>
''' <remarks></remarks>
Private Sub CaptureCameraCallback()
Me._isCameraRunning = True
Me._frame = New Mat()
Me._capture = New VideoCapture(_CAMERA_DEVICE)
Me._capture.Open(_CAMERA_DEVICE)
' Enable button if there is a camera feed
If _isCameraRunning Then
Me.BeginInvoke(New SetButtonCallback(AddressOf SetButtonStatus), True)
End If
While (_isCameraRunning)
Me._capture.Read(Me._frame)
Me._image = BitmapConverter.ToBitmap(Me._frame)
Me.captureFrame.BackgroundImage = Me._image
Me._image = Nothing
End While
End Sub
原代码保证相机是运行
while (isCameraRunning) ...
如果你不想循环,你应该使用一个
If isCameraRunning Then
_capture.Read(_frame)
...
End If
否则您可能会得到无效的帧。也许在尝试读取一个后测试是否有框架也足够了
_capture.Read(_frame)
If _frame IsNot Nothing Then
...
End If
如果仍然出现异常,请进行进一步测试以确保框架有效:
_capture.Read(_frame)
If _frame IsNot Nothing AndAlso _frame.Width > 0 AndAlso _frame.Height > 0 Then
...
End If
我在 VB Net Forms 应用程序中使用 OpenCVSharp3。我有这个与 C# 一起工作,但现在正在尝试转换为 VB Net。我想将网络摄像头提要显示到图片框。
''' <summary>
''' Feed from camera
''' </summary>
''' <remarks></remarks>
Private _capture As VideoCapture
''' <summary>
''' Frame from camera
''' </summary>
''' <remarks></remarks>
Private _frame As Mat
''' <summary>
''' Image
''' </summary>
''' <remarks></remarks>
Private _image As Bitmap
Private Sub CaptureCameraCallback()
Try
_isCameraRunning = True
_frame = New Mat()
_capture = New VideoCapture()
_capture.Open(_cameraDevice)
_capture.Read(_frame)
If _frame IsNot Nothing Then
While _isCameraRunning
_image = BitmapConverter.ToBitmap(_frame)
captureFrame.Image = _image
_image = Nothing
End While
End If
Catch ex As Exception
End Try
End Sub
Private Sub CaptureCamera()
_camera = New Thread(AddressOf CaptureCameraCallback)
_camera.Start()
End Sub
我有上面的代码可以将网络摄像头提要显示到图片框。但是当我 运行 它时,我得到以下异常:
A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll
异常发生在线路:
_image = BitmapConverter.ToBitmap(_frame)
我想我需要在某个地方进行处理,但不确定这是否是这里的问题。
正在运行的 C# 代码 -
// Declare required methods
private void CaptureCamera()
{
camera = new Thread(new ThreadStart(CaptureCameraCallback));
camera.Start();
}
private void CaptureCameraCallback()
{
try
{
frame = new Mat();
capture = new VideoCapture();
capture.Open(CameraDevice);
while (isCameraRunning)
{
capture.Read(frame);
image = BitmapConverter.ToBitmap(frame);
captureFrame.Image = image;
image = null;
}
}
catch (Exception)
{
MessageBox.Show("No camera found on device", "Start Stream",
MessageBoxButtons.OK, MessageBoxIcon.Error);
if (streamBtn.InvokeRequired)
{
streamBtn.Invoke(new MethodInvoker(delegate { streamBtn.Text = "Start"; }));
}
}
}
解决方案
''' <summary>
''' Get feed from camera
''' </summary>
''' <remarks></remarks>
Private Sub CaptureCameraCallback()
Me._isCameraRunning = True
Me._frame = New Mat()
Me._capture = New VideoCapture(_CAMERA_DEVICE)
Me._capture.Open(_CAMERA_DEVICE)
' Enable button if there is a camera feed
If _isCameraRunning Then
Me.BeginInvoke(New SetButtonCallback(AddressOf SetButtonStatus), True)
End If
While (_isCameraRunning)
Me._capture.Read(Me._frame)
Me._image = BitmapConverter.ToBitmap(Me._frame)
Me.captureFrame.BackgroundImage = Me._image
Me._image = Nothing
End While
End Sub
原代码保证相机是运行
while (isCameraRunning) ...
如果你不想循环,你应该使用一个
If isCameraRunning Then
_capture.Read(_frame)
...
End If
否则您可能会得到无效的帧。也许在尝试读取一个后测试是否有框架也足够了
_capture.Read(_frame)
If _frame IsNot Nothing Then
...
End If
如果仍然出现异常,请进行进一步测试以确保框架有效:
_capture.Read(_frame)
If _frame IsNot Nothing AndAlso _frame.Width > 0 AndAlso _frame.Height > 0 Then
...
End If