0

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));

                    }

}

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?

EDIT: 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.

xaria
  • 842
  • 5
  • 24
  • 47

2 Answers2

0

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

Images on second thread

WPF Dispatcher {"The calling thread cannot access this object because a different thread owns it."}

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.

Community
  • 1
  • 1
Boas Enkler
  • 12,264
  • 16
  • 69
  • 143
  • The problem is not with dispatcher not being able to access the object. If I add some user action in the thread, like MessageBox, the the image gets displayed. I tried the freeze also, it didn't solve the issue. – xaria Apr 12 '12 at 09:02
0

Try to use the Application.Current.Dispatcher instead of the one you used. I believe this will do the trick.

Cheers

BitKFu
  • 3,649
  • 3
  • 28
  • 43