[BUG] SkBitmap.Decode always fail some images, but using SKImage.FromEncodedData works

See original GitHub issue

Description

I am trying to load images in UWP with StorageFile, but some images always fail with a weird error message.

Code

using (var stream = await storageFile.OpenStreamForReadAsync())
{
    SKBitmap bitmap = SKBitmap.Decode(stream);
    // the rest
}

Expected Behavior

the image should be loaded

Actual Behavior

some pictures fail at the line of calling SKBitmap.Decode, with this error message:

Value cannot be null.
Parameter name: buffer

Basic Information

  • Version with issue: 2.80.3-preview.24
  • Last known good version: N/A
  • IDE: VS.NET
  • Platform Target Frameworks: 10.0.19041.0 - 10.0.17763.0
    • Android:
    • iOS:
    • Linux:
    • macOS:
    • Tizen:
    • tvOS:
    • UWP: 6.2.10
    • watchOS:
    • Windows Classic:
  • Target Devices: x64

** Details **

The manifest permission is all opened. For those images that fail, I can call GetThumbnailAsync properly.

Moreover, the stream is not null, I added the following code:

                        if (stream.CanRead && stream.CanSeek)
                        {
                            stream.Seek(0, SeekOrigin.Begin);
                        }

these lines pass, and the Decode still failed.

However, if I do this instead:

SKImage img = SKImage.FromEncodedData(stream);
SKBitmap bitmap = SKBitmap.FromImage(img);

Now those images can be loaded.

but why?

Screenshots

I uploaded one failed image in SO: https://stackoverflow.com/questions/66096184/why-does-skiasharp-fail-to-load-some-image-in-uwp-when-calling-skbitmap-decode

Reproduction Link

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
taublastcommented, Aug 9, 2021

Can it do anything with “some images” are thoses with specific metadata, causing this to happen?..

I have the following error happening with stable 2.80.3 on iOS 14.x (on iOS 12.x it’s not happening):

var bitmap = SKBitmap.Decode(stream);

crash boom bang:

{System.ArgumentNullException: Value cannot be null.
Parameter name: buffer
  at SkiaSharp.SKManagedStream.OnReadManagedStream (System.IntPtr buffer, System.IntPtr size) [0x0000d] in <870e9db956e7461cb750c2b6d9037f6f>:0 
  at SkiaSharp.SKManagedStream.OnRead (System.IntPtr buffer, System.IntPtr size) [0x00006] in <870e9db956e7461cb750c2b6d9037f6f>:0 
  at SkiaSharp.SKAbstractManagedStream.ReadInternal (System.IntPtr s, System.Void* context, System.Void* buffer, System.IntPtr size) [0x00015] in <870e9db956e7461cb750c2b6d9037f6f>:0 
  at (wrapper native-to-managed) SkiaSharp.SKAbstractManagedStream.ReadInternal(intptr,void*,void*,intptr)
  at (wrapper managed-to-native) SkiaSharp.SkiaApi.sk_codec_new_from_stream(intptr,SkiaSharp.SKCodecResult*)
  at SkiaSharp.SKCodec.Create (SkiaSharp.SKStream stream, SkiaSharp.SKCodecResult& result) [0x0003b] in <870e9db956e7461cb750c2b6d9037f6f>:0 
  at SkiaSharp.SKCodec.Create (System.IO.Stream stream, SkiaSharp.SKCodecResult& result) [0x00006] in <870e9db956e7461cb750c2b6d9037f6f>:0 
  at SkiaSharp.SKCodec.Create (System.IO.Stream stream) [0x00000] in <870e9db956e7461cb750c2b6d9037f6f>:0 
  at SkiaSharp.SKBitmap.Decode (System.IO.Stream stream) [0x0000e] in <870e9db956e7461cb750c2b6d9037f6f>:0 

Downgrading to 2.80.2 solved it…

3reactions
glintpursuitcommented, Jul 24, 2021

SKBitmap bitMap = SKBitmap.FromImage(SKImage.FromEncodedData(new MemoryStream(mediaFileBytearray)));

Does not work using SKImage too

skia version 2.80.3

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does SkiaSharp fail to load some image in UWP when ...
If I do this: SKImage img = SKImage.FromEncodedData(stream); SKBitmap bitmap = SKBitmap.FromImage(img);. It works for those photos!
Read more >
SKBitmap.Decode Method (SkiaSharp)
Decode a bitmap using the specified stream and destination image information. ... Returns. SKBitmap. The decoded bitmap, or null on error.
Read more >
Saving SkiaSharp bitmaps to files - Xamarin
(The SKCodec class is shown in the article Animating SkiaSharp Bitmaps in connection with decoding an animated GIF file.).
Read more >
SkImage.h - Android Code Search
Returns original SkImage if decoded in raster bitmap,. or if encoded in a stream. Returns nullptr if backed by GPU texture and copy...
Read more >
Skiasharp skbitmap encode. If Density is 2, then 1800 dips b
Decode always fail some images, but using SKImage. Worked fine w/ 2. SKFileWStream(@"C:\Temp\out. png")) { . Decode (sourceFile); var sourceActualWidth = …
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