BitmapImage的坑太大了。
WPF项目,用线程处理图片,完了通知主线程刷新UI显示处理之后的图片。只是简单的将处理后的图片赋值给BitmapImage.Source会报“调用线程无法访问此对象,因为另一个线程拥有该对象”的错误。必须使用BitmapFrame frame = BitmapFrame.Create(img)来处理。
上代码
1. 开线程
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
/*Your method*/
Thread th = new Thread(new ThreadStart(GenerateFilters));
th.Start(); //start a thread to process long run method
}
else
{
/*Your method*/
}
}
2. 处理图片方法
private void GenerateFilters()
{
/*Your long run method*/
RefreshFilter(); //call the UI to show the processed imag