Note that there are some explanatory texts on larger screens.

plurals
  1. POThe calling thread cannot access this object because a different thread owns it
    primarykey
    data
    text
    <p>Why I can't create CroppedBitmap in the following code? I got an exception:</p> <blockquote> <p>The calling thread cannot access this object because a different thread owns it.</p> </blockquote> <p>If I change the code to </p> <pre><code>CroppedBitmap cb = new CroppedBitmap(new WriteableBitmap(bf), new Int32Rect(1, 1, 5, 5)); </code></pre> <p>the exception is gone? why ?</p> <p>Code 1, an exception at <code>cb.Freeze()</code>:</p> <pre><code>public MainWindow() { InitializeComponent(); ThreadPool.QueueUserWorkItem((o) =&gt; { //load a large image file var bf = BitmapFrame.Create( new Uri("D:\\1172735642.jpg"), BitmapCreateOptions.None, BitmapCacheOption.None); bf.Freeze(); Dispatcher.BeginInvoke( new Action(() =&gt; { CroppedBitmap cb = new CroppedBitmap(bf, new Int32Rect(1,1,5,5)); cb.Freeze(); //set Image's source to cb.... }), DispatcherPriority.ApplicationIdle); } ); } </code></pre> <p>Code 2, works:</p> <pre><code> ThreadPool.QueueUserWorkItem((o) =&gt; { var bf = BitmapFrame.Create( new Uri("D:\\1172740755.jpg"), BitmapCreateOptions.None, //BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); bf.Freeze(); var wb = new WriteableBitmap(bf); wb.Freeze(); this.Dispatcher.Invoke( new Action(() =&gt; { var r = new Int32Rect(1, 1, 5, 5); CroppedBitmap cb = new CroppedBitmap(wb, r); cb.Freeze(); //set Image's source to cb.... Image.Source = cb; }), DispatcherPriority.ApplicationIdle); } ); </code></pre> <p>Code 3, works without WritableBitmap:</p> <pre><code>ThreadPool.QueueUserWorkItem((o) =&gt; { var bf = BitmapFrame.Create( new Uri("D:\\1172735642.jpg"), BitmapCreateOptions.None, //BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); bf.Freeze(); var bf2 = BitmapFrame.Create(bf); bf2.Freeze(); this.Dispatcher.Invoke( new Action(() =&gt; { var r = new Int32Rect(1, 1, 5, 5); BitmapSource cb = new CroppedBitmap(bf2, r); cb.Freeze(); //set Image's source to cb.... Image.Source = cb; }), DispatcherPriority.ApplicationIdle); } ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload