[WPF] The calling thread cannot access this object because a different thread owns it.
- by zunyite
Why I can't create CroppedBitmap in the following code ?
I got an exception : The calling thread cannot access this object because a different thread owns it.
public MainWindow()
{
InitializeComponent();
ThreadPool.QueueUserWorkItem((o) =>
{
//load a large image file
var bf = BitmapFrame.Create(
new Uri("D:\\1172735642.jpg"),
BitmapCreateOptions.DelayCreation | BitmapCreateOptions.IgnoreColorProfile,
BitmapCacheOption.None);
bf.Freeze();
Dispatcher.BeginInvoke(
new Action(() =>
{
CroppedBitmap cb = new CroppedBitmap(bf, new Int32Rect(1,1,5,5));
cb.Freeze();
//set Image's source to cb....
}),
System.Windows.Threading.DispatcherPriority.ApplicationIdle);
}
);
}