Skip to content

[Skia] BitmapSource.SetSource throws NullReferenceException for a stream Skia can't decode (6.x) #25549

Open

Description

@mikernet

Current behavior 🐛

On 6.x with the Skia renderer, BitmapSource.SetSource and SetSourceAsync throw a NullReferenceException when the stream holds data SkiaSharp can't decode: HEIC, AVIF, PDF or corrupt bytes. ImageFailed is never raised.

BitmapSource.skia.cs reads the size up front with SKCodec.Create(stream).Info, and SKCodec.Create returns null for such data. WriteableBitmap.SetSource fails the same way when it decodes the stream into its pixel buffer (SKImage.FromEncodedData(...) then .Info, in WriteableBitmap.skia.cs).

On WebAssembly the exception also blocks the browser decode that the stream load tries before Skia's, so a stream in a format the browser can decode, such as AVIF in Chrome or HEIC in Safari, can't be loaded either.

Expected behavior 🎯

SetSource doesn't throw, PixelWidth and PixelHeight stay 0, and the load raises ImageFailed, as on WinUI and on master. On WebAssembly the browser decode gets its chance first.

How to reproduce it (as minimally and precisely as possible) 🔬

var bitmap = new BitmapImage();
bitmap.ImageFailed += (s, e) => Debug.WriteLine($"ImageFailed: {e.ErrorMessage}");

// Any bytes Skia can't decode: a .heic, .avif or .pdf file, or garbage.
var bytes = new byte[] { 1, 2, 3, 4 };
bitmap.SetSource(new MemoryStream(bytes).AsRandomAccessStream()); // NullReferenceException

Workaround 🛠️

Catch the exception around SetSource/SetSourceAsync and treat it as a failed image. On WebAssembly, to let the browser decode a format Skia doesn't support, save the stream to a file and set UriSource to its ms-appdata:/// URI instead: the URI path doesn't read the size up front.

Renderer 🎨

  • Skia
  • Native

Affected platforms 📱💻🖥️

WebAssembly, Android, iOS, Desktop (Windows), Desktop (macOS), Desktop (X11), Desktop (Linux Framebuffer)

Uno.Sdk version (and other relevant versions) 📦

Uno.Sdk 6.7.30 (Uno.UI 6.7.135), seen on Skia desktop (Windows), WebAssembly and Android. Also seen on servicing/6.8 (5efcb98), on Skia desktop and WebAssembly.

master is not affected: there, BitmapSource and WriteableBitmap decode through ImageEncoderDecoder.Current.TryDecode and leave the size and pixels alone when it fails.

IDE version 🧑‍💻

Not relevant.

Anything else we need to know? 💬

None.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions