BitmapFrame.Create无法访问此对象

时间:2015-07-14 12:30:25

标签: c# invoke dispatcher bitmapframe bitmapencoder

我尝试将多个BitmapSource图片导出到png文件。这是我的代码:

主题调用:

var exportImagesThread = new Thread(ExportRangeOfImages);
exportImagesThread.Start();

功能:

private void ExportRangeOfImages()
{
     for (var currentFrame = exportFromFrame; currentFrame <= exportToFrame; currentFrame++)
     {
            var currentImage = Application.Current.Dispatcher.Invoke(() => (currentStream.Children[0] as Image).Source);
            var pngBitmapEncoder = new PngBitmapEncoder();
            var bitmapFrame = BitmapFrame.Create((BitmapSource) currentImage);

            Application.Current.Dispatcher.Invoke(() => pngBitmapEncoder.Frames.Add(bitmapFrame));
            var a = new FileStream(updatedDir + sequenceFile + "_" + frameNumber + ".png", FileMode.Create);
            pngBitmapEncoder.Save(a);
     }
}

这样做的时候,我正在接收Additional information: The calling thread cannot access this object because a different thread owns it.在线:var bitmapFrame = BitmapFrame.Create((BitmapSource) currentImage);

我错过了什么?

0 个答案:

没有答案