使用WPF中的Non-UI线程从Memory Stream设置图像控件的源 [英] Setting source of an Image control from Memory Stream using Non-UI thread in WPF

查看:139
本文介绍了使用WPF中的Non-UI线程从Memory Stream设置图像控件的源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从指纹扫描仪捕获图像,我想在Image控件中实时显示捕获的图像.

I am Capturing image from a finger print Scanner and i want to display the captured image live in an Image control.

//Onclick of a Button
 Thread WorkerThread = new Thread(new ThreadStart(CaptureThread));
 WorkerThread.Start();

因此,我如上所述创建了一个线程,并调用了从设备捕获图像并按如下所示设置Image控件源的方法.

So i created a thread as above and called the method that captures the image from the device and sets the source of the Image control as follows.

private void CaptureThread()
    {
        m_bScanning = true;
        while (!m_bCancelOperation)
        {
            GetFrame();
            if (m_Frame != null)
            {

                    MyBitmapFile myFile = new MyBitmapFile(m_hDevice.ImageSize.Width, m_hDevice.ImageSize.Height, m_Frame);
                    MemoryStream BmpStream = new MemoryStream(myFile.BitmatFileData);
                    var imageSource = new BitmapImage();
                    imageSource.BeginInit();
                    imageSource.StreamSource = BmpStream;
                    imageSource.EndInit();
                    if (imgLivePic.Dispatcher.CheckAccess())
                    {
                        imgLivePic.Source = imageSource;
                    }
                    else
                    {
                        Action act = () => { imgLivePic.Source = imageSource; };
                        imgLivePic.Dispatcher.BeginInvoke(act);
                    }
            }

            Thread.Sleep(10);
        }
        m_bScanning = false;
    }

现在,当我运行项目时,它会在行Action act = () => { imgLivePic.Source = imageSource; };上引发异常,说"由于其他线程拥有该对象,因此调用线程无法访问该对象". 我做了一些研究,发现如果我想在NON-UI线程上使用UI控件,我应该使用Dispatcher.Invoke方法,如您所见,但我仍然遇到相同的异常. 有人可以告诉我我在做什么错吗?

Now when i Run the project it throws an Exception on line Action act = () => { imgLivePic.Source = imageSource; }; Saying "The Calling thread Can Not Access this object because a different thread owns it". i did some research and i found out that if i want to use UI controls over a NON-UI thread i should use Dispatcher.Invoke method, which as you can see i have but i am still getting the same exception. can someone please tell me what am i doing wrong?

推荐答案

不一定需要在UI线程中创建BitmapImage.如果您Freeze它,以后将可以从UI线程访问它.因此,您还将减少应用程序的资源消耗.通常,如果可能,您应该尝试冻结所有Freezable,尤其是位图.

The BitmapImage does not necessarily need to be created in the UI thread. If you Freeze it, it will later be accessible from the UI thread. Thus you will also reduce the resource consumption of your application. In general, you should try to freeze all Freezables if possible, especially bitmaps.

using (var bmpStream = new MemoryStream(myFile.BitmatFileData))
{
    imageSource.BeginInit();
    imageSource.StreamSource = bmpStream;
    imageSource.CacheOption = BitmapCacheOption.OnLoad;
    imageSource.EndInit();
}

imageSource.Freeze(); // here

if (imgLivePic.Dispatcher.CheckAccess())
{
    imgLivePic.Source = imageSource;
}
else
{
    Action act = () => { imgLivePic.Source = imageSource; };
    imgLivePic.Dispatcher.BeginInvoke(act);
}

这篇关于使用WPF中的Non-UI线程从Memory Stream设置图像控件的源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆