Answer the question
In order to leave comments, you need to log in
What is the correct way to pass a WriteableBitmap object from one thread to another (BackgroundWorker)?
Hello, I'm writing a small WPF application that does some processing on an image and then displays it in a MainImage
. It does this for a long time, so I decided to shove all the processing code into a separate function and use a BackgroundWorker (for convenient progress display).
Simplified, everything looks like this:
This code is executed after getting the path to the image:
private void SelectImage ()
{
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += worker_DoWork;
worker.ProgressChanged += worker_ProgressChanged;
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.RunWorkerAsync(path); // path - строка (путь к изображению)
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
writableBitmap = new WriteableBitmap(new BitmapImage(new Uri((string)e.Argument))));
// Код обработки изображения ...
e.Result = writeableBitmap.Clone();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MainImage.Source = (WriteableBitmap)e.Result;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question