WPF에서 쓰레드를 이용하여 Image element에 접근할려고 합니다.
private void Capture()
{
System.Drawing.Size win_size = Screen.PrimaryScreen.Bounds.Size; // 화면전체 해상도
Bitmap bitmap = new Bitmap(win_size.Width, win_size.Height);
Graphics g = Graphics.FromImage(bitmap);
while (true)
{
g.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), new System.Drawing.Size(win_size.Width, win_size.Height));
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate
{
image.Source = bi;
}));
}
}
소스는 위와같이 bitmap 소스를 memorystream으로 변환후 이를 다시 BitmapImage를 이용하여 Image Element에 바인딩합니다.
쓰레드를 쓰지않고 바로 바인딩을 하면 정상적으로 바인딩이 되는데
쓰레드에서 Dispatcher.invoke를 이용하면 에러가 발생합니다.
이것은 왜 이런것일 까요??
