SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

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

Open sowenzhang opened this issue 4 years ago • 8 comments

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

sowenzhang avatar Feb 09 '21 15:02 sowenzhang

Are you able to attach the file here in a zip? These websites like to process them.

mattleibow avatar Feb 09 '21 16:02 mattleibow

yes, sorry, I was distracted after I quickly post the bug.

There are 2 photos in the folder Photos, the 4.jpg cannot be loaded but the other one works fine.

If you change the code to: using SKImage to read stream and then load into SKBitmap, then it works.

SkiaLoadTest.zip

sowenzhang avatar Feb 09 '21 17:02 sowenzhang

I am experiencing the exact same behaviour with some images exported from Photoshop. Do we have information why this is happening?

intenzive avatar Jul 15 '21 07:07 intenzive

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

Does not work using SKImage too

skia version 2.80.3

glintpursuit avatar Jul 24 '21 12:07 glintpursuit

Duplicate of #1551

mattleibow avatar Jul 29 '21 11:07 mattleibow

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..

taublast avatar Aug 09 '21 18:08 taublast

"Some images" are landscape-oriented images, portrait works. Hope it helps.

ThanhNg avatar Oct 20 '21 05:10 ThanhNg

I am getting the same errors for images with a high DPI (300). Images with lower ones (< 96) are working fine. Any ideas why this is the case?

Maz47 avatar Feb 21 '22 09:02 Maz47

Hi folks. Sorry it took forever to fix this - especially since it was so simple and totally my fault. I will try get a package out ASAP after this merges: https://github.com/mono/SkiaSharp/pull/2265

mattleibow avatar Sep 29 '22 04:09 mattleibow