2011-01-13 10 views
3

このコードでこの例外が発生します。 修正方法?WPF - 別のスレッドから "System.Windows.Controls.Image"を更新します

Excepton:

異なるスレッドが それを所有しているので、呼び出し元のスレッドがこの オブジェクトにアクセスすることはできません。

コード:

void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image) 
    { 
     IntPtr hBitMap = image.GetHbitmap(); 
     BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 

     Dispatcher.BeginInvoke((Action)(() => 
     { 
      labelX.Content = String.Format("X: {0}", Center.X); //OK Working 
      labelY.Content = String.Format("Y: {0}", Center.Y); //OK Working 
      pictureBoxMain.Source = bmaps; // THERE IS EXCEPTON 
     }), DispatcherPriority.Render, null); 

    } 

pictureBoxMainがSystem.Windows.Controls.Imageです。

あなたはそれがどのスレッドからアクセスできるようにしたBitmapSourceを凍結することができます

答えて

7

:あなたは画像をフリーズでき

void CamProc_NewTargetPosition(IntPoint Center, System.Drawing.Bitmap image) 
    { 
     IntPtr hBitMap = image.GetHbitmap(); 
     BitmapSource bmaps = Imaging.CreateBitmapSourceFromHBitmap(hBitMap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
     bmaps.Freeze(); 

     Dispatcher.BeginInvoke((Action)(() => 
     { 
      labelX.Content = String.Format("X: {0}", Center.X); 
      labelY.Content = String.Format("Y: {0}", Center.Y); 
      pictureBoxMain.Source = bmaps; 
     }), DispatcherPriority.Render, null); 

    } 
関連する問題