You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET绑定NewFrame事件报错,C#正常运行,求解决方案

VB.NET事件绑定报错问题(对应C#代码可正常运行)

报错信息

Error   3   Argument not specified for parameter 'sender' of 'Private Sub VideoCaptureDevice_NewFrame(sender As Object, eventArgs As AForge.Video.NewFrameEventArgs)'.  C:\Users\Admin\documents\visual studio 2012\Projects\Barcode Scanner using Webcam in VB.NET\Barcode Scanner using Webcam in VB.NET\Form1.vb 16  40  Barcode Scanner using Webcam in VB.NET
Error   2   Argument not specified for parameter 'eventArgs' of 'Private Sub VideoCaptureDevice_NewFrame(sender As Object, eventArgs As AForge.Video.NewFrameEventArgs)'.   C:\Users\Admin\documents\visual studio 2012\Projects\Barcode Scanner using Webcam in VB.NET\Barcode Scanner using Webcam in VB.NET\Form1.vb 16  40  Barcode Scanner using Webcam in VB.NET
Error   1   'Public Event NewFrame(sender As Object, eventArgs As AForge.Video.NewFrameEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.  C:\Users\Admin\documents\visual studio 2012\Projects\Barcode Scanner using Webcam in VB.NET\Barcode Scanner using Webcam in VB.NET\Form1.vb 16  9   Barcode Scanner using Webcam in VB.NET

我的VB.NET代码

Private Sub btnStart_Click_1(sender As Object, e As EventArgs) Handles btnStart.Click
    videoCaptureDevice = New VideoCaptureDevice(filterInfoCollection(cboCamera.SelectedIndex).MonikerString)
'报错代码行
    videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame
    videoCaptureDevice.Start()
End Sub

Private Sub VideoCaptureDevice_NewFrame(ByVal sender As Object, ByVal eventArgs As AForge.Video.NewFrameEventArgs)
    Dim bitmap As Bitmap = DirectCast(eventArgs.Frame.Clone(), Bitmap)
    Dim reader As New BarcodeReader()
    Dim result = reader.Decode(bitmap)
    If result IsNot Nothing Then
        txtBarcode.Invoke(New MethodInvoker(Sub()
                                                txtBarcode.Text = result.ToString()
                                            End Sub))
    End If
    pictureBox.Image = bitmap
End Sub

可正常运行的C#代码

private void btnStart_Click(object sender, EventArgs e)
{
    videoCaptureDevice = new VideoCaptureDevice(filterInfoCollection[cboCamera.SelectedIndex].MonikerString);
    videoCaptureDevice.NewFrame += VideoCaptureDevice_NewFrame;
    videoCaptureDevice.Start();
}
private void VideoCaptureDevice_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
    Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
    BarcodeReader reader = new BarcodeReader();
    var result = reader.Decode(bitmap);
    if (result != null)
    {
        txtBarcode.Invoke(new MethodInvoker(delegate()
        {
            txtBarcode.Text = result.ToString();
        }));
    }
    pictureBox.Image = bitmap;
}

问题原因

VB.NET与C#的事件绑定语法不同:C#使用+=绑定事件处理方法,但VB.NET不支持这种写法,当前代码会被编译器误认为是直接调用NewFrame事件(而非绑定处理方法),因此触发了“不能直接调用事件”和“参数缺失”的报错。

解决方案

将报错行替换为VB.NET标准的事件绑定语句:

AddHandler videoCaptureDevice.NewFrame, AddressOf VideoCaptureDevice_NewFrame

修改后的完整btnStart_Click_1方法:

Private Sub btnStart_Click_1(sender As Object, e As EventArgs) Handles btnStart.Click
    videoCaptureDevice = New VideoCaptureDevice(filterInfoCollection(cboCamera.SelectedIndex).MonikerString)
    AddHandler videoCaptureDevice.NewFrame, AddressOf VideoCaptureDevice_NewFrame
    videoCaptureDevice.Start()
End Sub

这样就能正确绑定事件处理方法,消除所有报错,逻辑与C#代码保持一致。

内容的提问来源于stack exchange,提问作者roy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 13:55:17