BitmapFrame.Create cannot access this object

129 Views Asked by At

I'm trying to export several BitmapSource images from to png files. Here's my code:

Thread Call:

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

Function:

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);
     }
}

When doing that I'm receiving Additional information: The calling thread cannot access this object because a different thread owns it. On line: var bitmapFrame = BitmapFrame.Create((BitmapSource) currentImage);

What am I missing?

0

There are 0 best solutions below