Repository navigation
Description
Current behavior 🐛
BitmapSource.SetSource and SetSourceAsync set PixelWidth and PixelHeight synchronously, on the UI thread. On master they get them from IImageEncoderDecoder.TryDecode(stream, null, null, ...): a full decode of the image, every frame of an animated GIF or WebP, whose pixels are thrown away once the first frame's size is read. The image is decoded again when it opens.
- For a 4000x2667 JPEG (2.4 MB) that is a 43 MB decode on every
SetSourcecall, a median of 76 ms on the UI thread on desktop, against under 1 ms to read the header. Re-running master'sSetSourcesteps (the two stream clones and the decode) on WebAssembly (Chrome, Release) takes 68 to 76 ms. - From reading the code, each of those decodes holds two 43 MB native buffers at once: the decode bitmap, and the copy
SKImage.FromBitmapmakes of it. On WebAssembly that memory comes out of the linear memory, which never shrinks. WriteableBitmap.SetSourcedecodes the stream twice: once for the size, then again into its pixel buffer.
This is a regression from 6.8, which read only the header (SKCodec.Create(stream).Info in BitmapSource.skia.cs). Master's backend-neutral codec seam, IImageEncoderDecoder, has no way to read an image's size without decoding it, so the size read moved to TryDecode.
Not regressions, and in 6.8 too:
SetSourcedoesn't reload the source. AnImagealready showing the bitmap keeps the previous picture, or nothing if the bitmap had no stream yet, and anImagethat shows it later gets the image opened from the previous stream.- When nothing else is subscribed to the source,
SetSourceAsyncopens it twice. On desktop aBitmapImagecan then decode twice, since the first open is only cancelled if it hasn't started yet, and aWriteableBitmapcopies its pixels into an image twice. When the source already has an image open,SetSourceAsynccompletes with that image, before the new one opens.
Expected behavior 🎯
SetSource reads the size from the image's header, as 6.8 did, and WriteableBitmap.SetSource decodes the stream once. Setting a stream replaces the image wherever the bitmap is shown, as WinUI's CImageSource::SetSource does, and SetSourceAsync opens the new source once and completes when it has opened.
How to reproduce it (as minimally and precisely as possible) 🔬
var bytes = File.ReadAllBytes("photo.jpg"); // e.g. 4000x2667
var stopwatch = Stopwatch.StartNew();
new BitmapImage().SetSource(new MemoryStream(bytes).AsRandomAccessStream());
Debug.WriteLine(stopwatch.ElapsedMilliseconds); // the time of a full decodeFor the reload: show a BitmapImage in an Image, then call SetSource on it; the Image doesn't change.
Workaround 🛠️
Loading the image through UriSource instead of SetSource: its size is set when it opens, not up front. For the reload, calling SetSourceAsync, which does reload.
Affected platforms 📱💻🖥️
WebAssembly, Android, iOS, tvOS, Desktop (Windows), Desktop (macOS), Desktop (X11), Desktop (Linux Framebuffer)
Uno.Sdk version (and other relevant versions) 📦
master as of 2026-10-16 (7872853). In 6.8, only the reload and SetSourceAsync parts.
IDE version 🧑💻
Not relevant.
Anything else we need to know? 💬
None.
Activity