mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.54k stars 542 forks source link

[QUESTION] SKBitmap.Decode(stream) will dispose this stream, feature or bug? #2263

Open xtuzy opened 2 years ago

xtuzy commented 2 years ago

I use SKBitmap.Decode(stream) to create a Bitmap, when i need continue use this stream, i found it is dispose.

sharex-20220928152228

sharex-20220928152307

Skiasharp Version: 2.88.2

LukePulverenti commented 2 years ago

Are you saying this was different in earlier versions?

xtuzy commented 2 years ago

no difference compare with 2.88.0, i just feel it is not right, stream should be disposed by user like this

using(var stream = ***)
{
  var bitmap = SKBitmap.Decode(stream);
}
dallonxu commented 2 years ago

@xtuzy

Just use SKData to wrap the stream, the stream will not be closed after SKBitmap.Decode.

using(var stream = ***)
{
    var skData = SKData.Create(stream);
    var bitmap = SKBitmap.Decode(skData);
    //stream will not be closed here
}
maxenko commented 1 year ago

This is a really bad design choice if not a bug.

mattleibow commented 1 year ago

I'll need to confirm, but I am pretty sure this is expected. The decode usually reads all the way to the end, and the stream may not always be rewindable. So there was not much point in keeping it open... However, the Stream overloads could maybe get an additional parameter to indicate whether or not to close the stream.

nifiz commented 1 week ago

@mattleibow Managing a Stream should be left up to the user, not a completely different function they call.