【问题标题】:Updating a textbox from ISampleGrabberCB从 ISampleGrabberCB 更新文本框
【发布时间】:2013-11-10 23:04:50
【问题描述】:

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

使用SampleCB 我的程序将图像转换为位图,然后将图像处理为条形码,然后解码。当我使用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;
    }

我得到的错误首先是A 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?

【问题讨论】:

    标签: c# winforms bitmap directshow directshow.net


    【解决方案1】:

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

    【讨论】:

    • 我将如何使用调用来执行此操作?我已经查看了一些 SO 问题,但我没有看到我应该做什么?
    • 您需要将窗口消息发布到您的 UI 线程,或者同步执行,例如使用您在回调线程中添加的队列,然后从 UI 线程中删除。
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多