0

I am trying to Update a WPF Image Control. Using VB.net.

I have tried many Methods using Delegates and Different Invoke Methods. However I am still getting the same Error:

An exception of type 'System.InvalidOperationException' occurred in WindowsBase.dll but was not handled in user code. Additional information: The calling thread cannot access this object because a different thread owns it.

bmp is the value I want to use to update the ImageControl called iMain.

Here is my Current Code:

 Dim uri As New Uri(strPhotoshootPath & "\" & strFilename)
        Dim bmp As New BitmapImage(uri)
        'Opens the Image so that it can be Displayed on the Control.

        iMain.Dispatcher.BeginInvoke(DispatcherPriority.Normal, New SetImageControlCallback(
            AddressOf SetImageControl), bmp)
        'Loads the Image into the Control. By Dispatching the Call to the UI Thread.

        uri = Nothing
        'Sets the Variables to Null
    Catch ex As Exception
        Debug.WriteLine("Message: " & ex.Message & " Source: " & ex.Source)
        MessageBox.Show("An Error Ocurred. " & ex.Message)
    End Try
End Sub

Delegate Sub SetImageControlCallback(ByVal bmp As BitmapImage)
Friend Sub SetImageControl(ByVal bmp As BitmapImage)
    iMain.Source = bmp
End Sub

Any Ideas in how to Accomplish this? Thanks.

Harry Sanderson
  • 343
  • 2
  • 15
  • 1
    Call `bmp.Freeze()` before passing it to BeginInvoke. This will make the BitmapImage cross-thread accessible. Freezing will however only work here if its a local file URI. Otherwise loading is done asynchronously, and the BitmapImage can't be frozen before the download has finished. – Clemens Feb 22 '16 at 20:32
  • @Clemens I read this too quickly... self deleting :) – Lynn Crumbling Feb 22 '16 at 20:38
  • That did the Trick Brilliant! :) – Harry Sanderson Feb 22 '16 at 21:05

0 Answers0