SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

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

Open xtuzy opened this issue 2 years ago • 5 comments

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

xtuzy avatar Sep 28 '22 07:09 xtuzy

Are you saying this was different in earlier versions?

LukePulverenti avatar Sep 28 '22 18:09 LukePulverenti

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);
}

xtuzy avatar Sep 28 '22 22:09 xtuzy

@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
}

dallonxu avatar Nov 04 '22 08:11 dallonxu

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

maxenko avatar Aug 21 '23 07:08 maxenko

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.

mattleibow avatar Aug 21 '23 08:08 mattleibow