2011-01-13 3 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); 

    } 
관련 문제