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.