i'm stucked at some point which seemed to be easy....
I have a View with an image control:
<Image x:Name="ImageControl" Source="{Binding DisplayedImage, UpdateSourceTrigger=PropertyChanged }" HorizontalAlignment="Left"
Margin="0,0,0,0" Stretch="Fill" VerticalAlignment="Bottom"
Width="800" Height="300" Grid.Column="3" Grid.Row="3" />
And creating the Datacontext in the background :
public MainWindow()
{
InitializeComponent();
MainViewModel VM = new MainViewModel();
ImageControl.DataContext = VM;
}
In my Viewmodel i have the property as well as doing something on the backgroundworker. In the backgroundworker i raise an event and the event handler should update the property which should result in an update of the image:
public event EventHandler<NewImageToShowEventArgs> NewImageToShowEvent;
public class NewImageToShowEventArgs : EventArgs
{
public BitmapSource NewImage { get; set; }
public Bitmap NewImage2 { get; set; }
}
public MainViewModel()
{
uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
int Result = GetCamera();
//MessageBox.Show(Convert.ToString( Result));
// Assign Each Task with GrabImage helper
Grabbgw.DoWork += DoGrab;
// Grabbgw.RunWorkerCompleted += GrabWorkerCompleted;
bgws.Add(Grabbgw);
//assign Events
NewImageToShowEvent += NewImageToShow;
StartGrab();
}
private void NewImageToShow(object sender, NewImageToShowEventArgs e)
{
try
{
Application.Current.Dispatcher.Invoke((new Action(() =>
{
//DisplayedImage.Freeze(); // through a different error
DisplayedImage = e.NewImage.Clone();
// DisplayedImage2 = e.NewImage2;
})));
//Task.Factory.StartNew(() => DisplayedImage2 = e.NewImage2, CancellationToken.None, TaskCreationOptions.None, uiScheduler);
//App.Current.MainWindow.Dispatcher.BeginInvoke(new Action(() =>
//{
// DisplayedImage = e.NewImage.Clone();
// DisplayedImage2 = e.NewImage2;
//}));
}
catch (Exception ex) {
MessageBox.Show(ex.Message);
}
}
NewImageToShowEventArgs args = new NewImageToShowEventArgs();
args.NewImage = rawImage.bitmapsource.Clone() ;
args.NewImage2 = rawImage.bitmap;
NewImageToShowEvent?.Invoke(this, args);
I tried several things but all results in: "The calling thread cannot access this object because a different thread owns it." I searched now for hours without any results. I mean i tried the suggestions but they doesn't solved the problem. General Info: I get the image from a camera as either Bitmap or BitmapSource.
In the Output window it shows additional errors:
Exception thrown: 'System.InvalidOperationException' in WindowsBase.dll Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
I hope somebody has an idea why i still get the error.
Thanks!
All the best, Jan