0

我目前有一个工作程序,它显示来自我的网络摄像头的预览并使用 ISampleGrabberCB 界面。

使用SampleCB我的程序将图像转换为位图,然后将图像处理为条形码,然后对其进行解码。当我使用 a 显示结果时,这非常有效,MessageBox但是当我希望使用此结果在主窗体上编辑文本框时,我在启动程序时遇到了一些错误。

我正在尝试使用ISampleGrabberCB界面中的以下代码更新我的文本框:

public int SampleCB(double sampletime, IMediaSample sample)
    {
        if (sample == null)
        {
            return -1;
        }
        try
        {
            int length = sample.GetActualDataLength();
            IntPtr buffer;
            BitmapData bitmapData = new BitmapData();

            Form1 f1 = new Form1("", "", "");


            if (sample.GetPointer(out buffer) == 0 && length > 0)
            {
                Bitmap bitmapOfFrame = new Bitmap(width, height, pitch, PixelFormat.Format24bppRgb, buffer);


            }

方法changeTextBox1在我的主窗体中,如下:

public void changeTextBox1(string text)
    {
        textBox1.Text = text;
    }

我得到的错误是 firstA device attached to the system in not functioning properly然后no such supported interface。这似乎只在我使用这Form1 f1 = new Form1("","","");条线时发生。

所以正如我所说,如果我删除该行Form1 f1 = new Form1("","","");并替换changeTextBox1(result.Text);MessageBox.Show(result.Text.ToString());这个作品。

我将如何更新文本框而不是使用 MessageBox?

4

1 回答 1

2

您应该在主 UI 线程中进行 UI 更改,但是您的回调 SampleCB 是从另一个系统线程调用的,因此会出现错误。使用消息发布或其他方式将数据从回调线程安全地传递到主 UI 线程,并使用主 UI 线程中的新数据更新 UI。

于 2013-11-11T08:30:19.673 回答