WPF成像问题 [英] WPF imaging problems

查看:146
本文介绍了WPF成像问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的我有一个wpf图像对象,它会显示实时图像。所以我用一个计时器来刷新图像。

Okay I have a an wpf image object, and it displays live images. So I have used a timer to refresh the image.

    public void LoadLiveImage()
        {

System.Windows.Media.PixelFormat pf = PixelFormats.Bgr24;
                    int stride = 4 * ((24 * cameraFrame.img_width + 31) / 32); 
                     BitmapSource bmpImage=  BitmapSource.Create(cameraFrame.img_width, cameraFrame.img_height, cameraFrame.img_width, cameraFrame.img_height, pf, null, cameraFrame.img_pixel, stride);
                    RemoteCameraImage.Source = bmpImage;
}

 void dispatcherTimer_Tick(object sender, EventArgs e)
        {
             LoadLiveImage();           

        }

没问题,这个工作正常。
但是,我试图将其移动到一个线程并且不显示任何图像。

No issues, this is working fine. However, I tried to move this to a thread and no image is displayed.

 private void showLiveImage()
        {
                while (this.isCameraViewOpen)
                {
                 if (RemoteCameraImage.Dispatcher.CheckAccess())
                        {
                            System.Windows.Media.PixelFormat pf = PixelFormats.Bgr24;
                            int stride = 4 * ((24 * cameraFrame.img_width + 31) / 32); 
                            BitmapSource bmpImage = BitmapSource.Create(cameraFrame.img_width, cameraFrame.img_height, cameraFrame.img_width, cameraFrame.img_height, pf, null, cameraFrame.img_pixel, stride);
                            RemoteCameraImage.Source = bmpImage;

                            System.Threading.Thread.Sleep(5);
                        }
                        else
                            this.RemoteCameraImage.Dispatcher.Invoke(DispatcherPriority.Normal, new ImageUpdater(this.showLiveImage));

                    }

}

showLiveImage isrunning作为一个线程。收到图像,没有问题。我通过将img_pixel数组保存到bmp文件并生成文件来测试。只是图像没有显示。所以我在分配源之后放置了一个消息框,然后我就可以看到Image对象上的图像了。所以我认为问题是我增加了睡眠时间,但即使图像也没有刷新。可能是什么问题?

The showLiveImage isrunning as a thread. The image is received, there is not problem in that. I tested by saving the img_pixel array to a bmp file and file is generated. Just that the image is not displayed on. So I put a messagebox to be shown after the source is assigned, and then I m able to see the image on Image object. SO I think the problem I increased the Sleep time, but even the image is not refreshed. WHAT could be the issue?

编辑:
将更新图像的代码移动到另一个函数后,它可以正常工作。我使用BeginInvoke()而不是调用一个正常工作。

After moving the code which was updating the image to another function, it works fine. And I used BeginInvoke() instead of invoke an all works fine.

推荐答案

前一段时间我有类似的问题,我发现这个问题Stackoverflow,它为我解决了这个问题

Sometime ago I had similiar problems and I found this on Stackoverflow, which sovled it for me

第二个帖子上的图片

WPF Dispatcher {"调用线程无法访问此对象,因为一个不同的线程拥有它。}


如果您创建了$ b后无意修改图像$ b你可以使用Freezable.Freeze将其冻结,然后在调度程序委托中分配给
GeneratedImage(BitmapImage变为
只读,因此冻结导致线程安全)。另一个
选项是将图像加载到
后台线程的MemoryStream中,然后在
的UI线程上创建BitmapImage,该调度程序委托与该流和StreamSource属性
$ BitmapImage。

If you have no intention of modifying the image once you have created it you can freeze it using Freezable.Freeze and then assign to GeneratedImage in the dispatcher delegate (the BitmapImage becomes read-only and thus threadsafe as a result of the Freeze). The other option would be to load the image into a MemoryStream on the background thread and then create the BitmapImage on the UI thread in the dispatcher delegate with that stream and the StreamSource property of BitmapImage.

这篇关于WPF成像问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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