1

I need to operate on bitmap pixels asynchronously. There is Bitmap and WriteableBitmap. I need something like:

public async Task<Bitmap> ProcessBitmap(Bitmap bitmap)
{
   // operate on bitmap asynchronously
   return newBitmap;
}

I wanted to use WriteableBitmap and convert it to stream and use stream async methods but PixelBuffer property seems unavailable in both WPF and WinForms. How should I do this? I could also use Parallel.For in some scenarios but how would I get result then?

2
  • 1
    So you can do something like var files = new [] { "c:\file1.bmp", "c:\file2.bmp" }; var tasks = files.Select(x => ProcessBitmap(x)); await Task.WhenAll(tasks). The WhenAll will wait for all tasks started to execute (here is you parallelism. Commented Jul 18, 2016 at 14:47
  • Look into using the Parallel library in .NET. It will allow you to utilize all the cores of the CPU to do image processing. msdn.microsoft.com/en-us/library/dd460720(v=vs.110).aspx Commented Jul 18, 2016 at 14:49

2 Answers 2

5

Image processing is almost entirely CPU-bound. There's no asynchronous I/O you could exploit.

Do not confuse I/O-bound code (where async works great) with CPU-bound code (where async doesn't give you any benefits, really).

Sign up to request clarification or add additional context in comments.

1 Comment

Probably, the question point is that the processing should run on a different thread not to block the UI thread. So asynchronously should be read as on a parallel thread, I suppose.
1

With Task.Run you can send the CPU-intensive work to ThreadPool, so your thread is free to do other work while awaiting:

public async void Button_Click(object sender, RoutedEventArgs e) {
    ...
    var processedBitmap = await ProcessBitmap(bitmap);
    ...
}

public async Task<Bitmap> ProcessBitmap(Bitmap bitmap) {
    //on bitmap asynchronously
    await Task.Run(() => {
        return DoActualBitmapProcessing(bitmap);
    }
} 

1 Comment

Where is the return statement for the ProcessBitmap function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.