Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 2.84.0-preview.4: SKCodec.Create - Value cannot be null. Parameter name: buffer #1551

Closed
AndersMad opened this issue Nov 11, 2020 · 23 comments · Fixed by #2265
Closed
Milestone

Comments

@AndersMad
Copy link

Description

Exception "Value cannot be null. Parameter name: buffer" on SKCodec.Create - stack:

SkiaSharp.SKManagedStream.OnReadManagedStream(IntPtr buffer, IntPtr size)
SkiaSharp.SKAbstractManagedStream.ReadInternal(IntPtr s, Void* context, Void* buffer, IntPtr size)
SkiaSharp.SKCodec.Create(SKStream stream, SKCodecResult& result)

This only happens on a few images - attached one.

Code

// inStream is a filestream
using var imgStream = new SKManagedStream(inStream);
using var codec = SKCodec.Create(imgStream, out SKCodecResult result);

Basic Information

Version with issue: 2.84.0-preview.4
Last known good version: 2.84.0-preview.1
IDE: Visual Studio 2019
Platform Target Frameworks: .NETFramework 4.8

Image File Sample

file-22

@AndersMad
Copy link
Author

I did this as a workaround - go through SKData instead of creating the codec from a stream:

using var imgStream = new SKManagedStream(inStream);
using var skData = SKData.Create(inStream);
using var codec = SKCodec.Create(skData);

@intenzive
Copy link

@AndersMad, you've just saved my day! Thank you!

@dstj
Copy link

dstj commented Jul 16, 2021

I've got the same error after upgrading SkiaSharp.NativeAssets.Linux from v2.80.2 to v2.80.3. The crash occurs in production using Docker mcr.microsoft.com/dotnet/aspnet:5.0-alpine as base image.

I do not control the calls to Skia as I'm using a library that uses it (i.e. Aspose.Words), so I cannot try to proposed workaround. :/

at SkiaSharp.SKManagedStream.OnReadManagedStream(IntPtr buffer, IntPtr size)
at SkiaSharp.SKManagedStream.OnRead(IntPtr buffer, IntPtr size)
at SkiaSharp.SKAbstractManagedStream.ReadInternal(IntPtr s, Void* context, Void* buffer, IntPtr size)
 - Process exited with status 134
 - State changed from up to crashed

Could this be related to this PR #1510

@mlancione
Copy link

This null exception also occurs in v2.80.3 on iOS when calling SKBitmap.Decode( stream ).

@glintpursuit
Copy link

We are getting "Value cannot be null" error on Android while creating bitmap.

SKBitmap bitMap = SKBitmap.Decode(new MemoryStream(mediaFileBytearray));

@glintpursuit
Copy link

Same error even from SKimage

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

@glintpursuit
Copy link

rolled back to previous version 2.80.2

@mattleibow
Copy link
Contributor

Is it possible to attach an image in a zip file? GitHub does some processing and the image is modified.

Also, could you give the latest 2.88 preview a try.

@mattleibow
Copy link
Contributor

Looks like these are related.

Duplicate of #1621

@TopperDEL
Copy link

TopperDEL commented Aug 11, 2021

Using version 2.88-preview.120 did not solve this issue for me on android. It is working by rolling back to 2.80.2, though.

@AndersMad
Copy link
Author

sry @mattleibow - I didn't see that zip request, so here's a beautiful girl with data problems for you :) beautiful-girl-1551.zip

Djohnnie added a commit to Djohnnie/Photo-Carousel that referenced this issue Aug 25, 2021
Djohnnie added a commit to Djohnnie/Photo-Carousel that referenced this issue Aug 25, 2021
Djohnnie added a commit to Djohnnie/Photo-Carousel that referenced this issue Aug 25, 2021
@ossipoff
Copy link

Also got this error after updating to 2.80.3. It seems to happen with all photos taken with Samsung Galaxy 8 phone

@npostma
Copy link

npostma commented Oct 8, 2021

I can confirm. Skisharp is not working with images taken from my Samsung S8 (Other devices have no issue android or iOS)

System.ArgumentNullException: Value cannot be null.
Parameter name: buffer
  at SkiaSharp.SKManagedStream.OnReadManagedStream (System.IntPtr buffer, System.IntPtr size) [0x0000d] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKManagedStream.OnRead (System.IntPtr buffer, System.IntPtr size) [0x00006] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKAbstractManagedStream.ReadInternal (System.IntPtr s, System.Void* context, System.Void* buffer, System.IntPtr size) [0x00015] in <ab66cf33abf94e6f9c6728684fce9316>: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 <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKCodec.Create (System.IO.Stream stream, SkiaSharp.SKCodecResult& result) [0x00006] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKCodec.Create (System.IO.Stream stream) [0x00000] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKBitmap.Decode (System.IO.Stream stream) [0x0000e] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at <mypath>.BitmapExtensions.LoadBitmapFromFile (System.Type type, System.String filePath) [0x00018] in <mypath>\BitmapExtensions.cs:35 
  at <mypath>.LoadBitmap () [0x0004b] in <mypath>.someViewModel.cs:642 

@aloksharma1
Copy link

This bug still persists in version 2.88 prev .155 any updates or workarounds?

@hosseinitabar
Copy link

For me, the workaround @AndersMad said worked. But my images are now turned 90 degrees.

@DiebBlue
Copy link

DiebBlue commented Feb 7, 2022

I did this as a workaround - go through SKData instead of creating the codec from a stream:

using var imgStream = new SKManagedStream(inStream);
using var skData = SKData.Create(inStream);
using var codec = SKCodec.Create(skData);

Since update from 2.82 to 2.83 have have the same problems with samsung devices.
But this workaround worked for me also. Thanks a lot :-)

@replaysMike
Copy link

thanks for that solution @AndersMad - I ran into this same error with certain images via SKBitmap.Decode(stream):

Stream? stream = /*......*/;
var bitmap = SKBitmap.Decode(stream); // NOPE, error!
var image = SKImage.FromBitmap(bitmap);
var outputData = image.Encode(SKEncodedImageFormat.Png, 100);

changed to this to fix:

Stream? stream = /*......*/;
using var inputStream = new SKManagedStream(stream);
using var inputData = SKData.Create(inputStream);
var bitmap = SKBitmap.Decode(inputData); // now it works
var image = SKImage.FromBitmap(bitmap);
var outputData = image.Encode(SKEncodedImageFormat.Png, 100);

@theolivenbaum
Copy link

Any changes on this? Still happening on latest preview

@codeBestard
Copy link

codeBestard commented Apr 14, 2022

Any changes on this? Still happening on latest preview

Yes, still has issue in 2.88.0-preview.232

Fusion86 added a commit to Fusion86/Avalonia that referenced this issue Apr 21, 2022
@BenjaminMichaelis
Copy link

I had the same issue with this photo that I have zipped up that I got from google. I have another if needed as 2 of my 12 testing photos threw this error Value cannot be null. Parameter name: buffer. The fix was instead of doing this:

            Stream photoContentStream; // Stream passed in with data, just noting type here for clarity.
            using var stream = new SKManagedStream(photoContentStream, false);
            SKBitmap? bitmap = SKBitmap.Decode(stream);

Solution was this:

            using var stream = new SKManagedStream(photoContentStream, false);
            SKImage img = SKImage.FromEncodedData(stream);
            SKBitmap? bitmap = SKBitmap.FromImage(img);

Any status on the core reasoning behind this? I am using Version="2.88.0"

14b05c00-558d-40af-93aa-608a1a3441db.zip

@shehrozeee
Copy link

For me, the workaround @AndersMad said worked. But my images are now turned 90 degrees.

Were you able to fix the 90 deg problem ?
The problem only occurs if you process load an image taken by the device camera. Orientation information is not processed I guess

@jamers99
Copy link

Also ran into this on iOS and Android in Xamarin.Forms.

Doing var bitmap = SKBitmap.Decode(stream) (where stream is a MemoryStream) and I get the following exception.

System.ArgumentNullException
  Message=Value cannot be null.
Parameter name: buffer
  at SkiaSharp.SKManagedStream.OnReadManagedStream (System.IntPtr buffer, System.IntPtr size) [0x0000d] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKManagedStream.OnRead (System.IntPtr buffer, System.IntPtr size) [0x00006] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKAbstractManagedStream.ReadInternal (System.IntPtr s, System.Void* context, System.Void* buffer, System.IntPtr size) [0x00015] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at (wrapper native-to-managed) SkiaSharp.SKAbstractManagedStream.ReadInternal(intptr,void*,void*,intptr)
  at (wrapper managed-to-native) SkiaSharp.SkiaApi.sk_codec_get_pixels(intptr,SkiaSharp.SKImageInfoNative*,void*,intptr,SkiaSharp.SKCodecOptionsInternal*)
  at SkiaSharp.SKCodec.GetPixels (SkiaSharp.SKImageInfo info, System.IntPtr pixels, System.Int32 rowBytes, SkiaSharp.SKCodecOptions options) [0x0009e] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKCodec.GetPixels (SkiaSharp.SKImageInfo info, System.IntPtr pixels) [0x0000a] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKBitmap.Decode (SkiaSharp.SKCodec codec, SkiaSharp.SKImageInfo bitmapInfo) [0x0001f] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKBitmap.Decode (SkiaSharp.SKCodec codec) [0x0002f] in <ab66cf33abf94e6f9c6728684fce9316>:0 
  at SkiaSharp.SKBitmap.Decode (System.IO.Stream stream) [0x0001c] in <ab66cf33abf94e6f9c6728684fce9316>:0 

Doing @BenjaminMichaelis's suggestion above worked.

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

Thanks!!

grokys added a commit to AvaloniaUI/Avalonia that referenced this issue Jul 26, 2022
Use the workaround from mono/SkiaSharp#1551 (comment) to load bitmaps.

Fixes #7773
@mattleibow mattleibow modified the milestones: v2.88.1, v2.88.x Planning Sep 11, 2022
@mattleibow mattleibow modified the milestones: v2.88.x Planning, v2.88.3 Sep 29, 2022
@mattleibow
Copy link
Contributor

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: #2265

@ghost ghost locked as resolved and limited conversation to collaborators Oct 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.