SixLabors / ImageSharp

:camera: A modern, cross-platform, 2D Graphics library for .NET
https://sixlabors.com/products/imagesharp/
Other
7.39k stars 851 forks source link

'System.NotSupportedException' in System.Private.CoreLib.dll #2004

Closed aecceyhan closed 2 years ago

aecceyhan commented 2 years ago

ImageSharp version

SixLabors.ImageSharp 2.0.0

Other ImageSharp packages and versions

SixLabors.ImageSharp 2.0.0

Environment (Operating system, version and so on)

macOS Monterey M1

.NET Framework version

net5.0

Description

When I try to save an image on stream I get "'System.NotSupportedException' in System.Private.CoreLib.dll" exception.

Steps to Reproduce

 public static Stream GetReducedImage(int width, int height, Stream resourceImage)
        {
            IImageFormat format;
            using (Image image = Image.Load(resourceImage, out format))
            {
                image.Mutate(x => x.Resize(width, height));
                image.Save(resourceImage, format);
                return resourceImage;
            }
}

Images

No response

tocsoft commented 2 years ago

@aecceyhan any chance you can provide a stack trace?

aecceyhan commented 2 years ago

@tocsoft

I add this to get stack trace:

try
                {
                    image.Save(resourceImage, format);
                }
                catch
                {
                    Console.WriteLine(
                        new System.Diagnostics.StackTrace().ToString()
                        );
                }
 at ImageResizer.Worker.GetReducedImage(Int32 width, Int32 height, Stream resourceImage)
   at ImageResizer.Worker.ImageResizer(ImageUploadEvent event)
   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
   at ImageResizer.Worker.ImageResizer(ImageUploadEvent event)
   at ImageResizer.Worker.ExecuteAsync(CancellationToken stoppingToken)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.ExecutionContextCallback(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext(Thread threadPoolThread)
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AsyncStateMachineBox`1.MoveNext()
   at System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(IAsyncStateMachineBox box, Boolean allowInlining)
   at System.Threading.Tasks.Task.RunContinuations(Object continuationObject)
   at System.Threading.Tasks.Task.FinishContinuations()
   at System.Threading.Tasks.Task.TrySetResult()
   at System.Threading.Tasks.Task.DelayPromise.CompleteTimedOut()
   at System.Threading.Tasks.Task.DelayPromise.<>c.<.ctor>b__1_0(Object state)
   at System.Threading.TimerQueueTimer.CallCallback(Boolean isThreadPool)
   at System.Threading.TimerQueueTimer.Fire(Boolean isThreadPool)
   at System.Threading.TimerQueue.FireNextTimers()
   at System.Threading.TimerQueue.AppDomainTimerCallback(Int32 id)

Screenshot at Feb 16 13-20-42

JimBobSquarePants commented 2 years ago

Does it work with .NET 6?

I’m curious to see if it has anything to do with the difference in M1 support between the two framework versions.

tocsoft commented 2 years ago

~Whats ImageResizer? is this your code or a third party library?~

Scratch that, its obviously your code as your sample above it the final method.

JimBobSquarePants commented 2 years ago

Why does the stacktrace originate there though? There’s no reference to ImageSharp in the trace.

tocsoft commented 2 years ago

Run the below code instead to get the stack trace of the error not the stack trace of the the catch.

try
{
    image.Save(resourceImage, format);
}
catch(Exception ex)
{
    Console.WriteLine(ex.StackTrace);
}
aecceyhan commented 2 years ago

@tocsoft

at System.IO.MemoryStream.set_Capacity(Int32 value)
   at System.IO.MemoryStream.EnsureCapacity(Int32 value)
   at System.IO.MemoryStream.Write(ReadOnlySpan`1 buffer)
   at SixLabors.ImageSharp.Formats.Png.PngEncoderCore.Encode[TPixel](Image`1 image, Stream stream, CancellationToken cancellationToken)
   at SixLabors.ImageSharp.Formats.ImageEncoderUtilities.Encode[TPixel](IImageEncoderInternals encoder, Image`1 image, Stream stream)
   at SixLabors.ImageSharp.Formats.Png.PngEncoder.Encode[TPixel](Image`1 image, Stream stream)
   at SixLabors.ImageSharp.Image.EncodeVisitor.Visit[TPixel](Image`1 image)
   at SixLabors.ImageSharp.Image`1.Accept(IImageVisitor visitor)
   at SixLabors.ImageSharp.Advanced.AdvancedImageExtensions.AcceptVisitor(Image source, IImageVisitor visitor)
   at SixLabors.ImageSharp.Image.Save(Stream stream, IImageEncoder encoder)
   at SixLabors.ImageSharp.ImageExtensions.Save(Image source, Stream stream, IImageFormat format)
   at ImageResizer.Worker.GetReducedImage(Int32 width, Int32 height, Stream resourceImage) in [/Users/aec/Documents/Centrum/ImageResizer/Worker.cs]():line 156

image

JimBobSquarePants commented 2 years ago

That stacktrace originates in MemoryStream.Capacity and not in ImageSharp.

A NotSupportedException is thrown there when the MemoryStream has either been:

aecceyhan commented 2 years ago

Thanks, @JimBobSquarePants

Do you have any recommendations?

tocsoft commented 2 years ago

basically it looks like there is an issue in your code with how you are handling/creating the resourceImage stream you want to look to make sure you are not creating the MemoryStream with a capacity or with a fixed buffer i.e. create the stream with new MemoryStream() rather than new MemoryStream(new byte [0]) or new MemoryStream(0) etc.

Or you not calling stream.Dispose() either directly or indirectly via a using block/expression.

Ultimately it look like you have some dodgy code elsewhere in your application and there is no issue with ImageSharp.