question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Should be able to load bitmaps in non-UI threads

See original GitHub issue

Proposal: Should be able to load bitmaps in non-UI threads

Summary

We should be able to load bitmaps in non-UI threads. I’m talking about BitmapImage and WriteableBitmap classes.

Rationale

Forcing loading of bitmaps in the UI thread is a huge bottleneck when you need to deal with hundreds of bitmaps.

I completely understand forcing drawing the bitmaps only in the UI thread, but just loading them should not be bound in the UI thread.

Important Notes

Simply doing var bmp = new BitmapImage(); on a non UI thread throws an exception. This should not be the case. This piece of code should work:

Task.Run(async () => {
	var root = ApplicationData.Current.LocalFolder.Path + "\\test.png";
	var bmp = new BitmapImage();
	var file = await StorageFile.GetFileFromPathAsync(root);
	var stream = await file.OpenAsync(FileAccessMode.Read);
	bmp.SetSource(stream);
});

Or, give me an equivalent, like, static BitmapImage BitmapImage.LoadFromStream(...).

For WriteableBitmap, again, doing var bmp = new WriteableBitmap(100, 100); on a non-UI thread will throw an exception. Should not be the case.

Executing the following in a non-UI thread should work.

public static BitmapSource soft_bmp_to_bmp(SoftwareBitmap soft_bmp) {
	release.assert(soft_bmp != null);
	WriteableBitmap bmp = new WriteableBitmap(soft_bmp.PixelWidth, soft_bmp.PixelHeight);
	soft_bmp.CopyToBuffer( bmp.PixelBuffer);
	return bmp;
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:6

github_iconTop GitHub Comments

1reaction
JohnnyWestlakecommented, Apr 15, 2020

As mention in the Optimize Image Resources section here https://docs.microsoft.com/en-gb/windows/uwp/debug-test-perf/optimize-animations-and-media#optimize-image-resources , decoding is actually already done off the UI thread even if you create the BitmapImage itself on the UI thread, as long as you use SetSourceAsync(…)

To preserve XAML optimisations you should also make sure the BitmapImage is in the UI tree before calling SetSourceAsync, to make sure the image is decoded at a preferred resolution for it’s target display area.

0reactions
jtorjocommented, May 5, 2020

@codenone Sorry for the late reply. It would be really useful to allow SetSourceAsync from a non-UI thread. This way, I don’t have to call into the UI thread, while caching bitmaps.

Adding more non-UI-thread APIs means we now need to see how to make it clear to developers what APIs do or don’t require the UI thread.

Agreed 100%. Personally, I think loading bitmaps in non-UI thread pretty much means I’ll add them to the UI programmatically, not via XAML.

Also , please don’t forget about WriteableBitmap 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Process image on non-ui thread
Objects like WriteableBitmap must be used on the thread they were created. So you have to allocate it on the other thread.
Read more >
[uwp][c#] is there a way to load a bitmap, NOT on the main ...
I have a .png file, and want to load it as a WriteableBitmap. However, if I don't do this on the main thread,...
Read more >
Better performance through threading | App quality
Understanding how it works can help you design your app to use the main thread for the best possible performance.
Read more >
Handling bitmaps | Android Developers
Loading bitmaps on the UI thread can degrade your app's performance, causing slow responsiveness or even ANR messages. It is therefore important ...
Read more >
Android Threading: All You Need to Know
This, in my opinion, would be the best option. Since IntentService doesn't attach to any activity and it runs on a non-UI thread,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found