ImageSharp icon indicating copy to clipboard operation
ImageSharp copied to clipboard

SaveAsWebpAsync throws IndexOutOfRangeException in UWP published app

Open brunomuratore opened this issue 1 year ago • 4 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [X] I have verified that I am running the latest version of ImageSharp
  • [X] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [X] I have searched open and closed issues to ensure it has not already been reported

ImageSharp version

2.1.6

Other ImageSharp packages and versions

none

Environment (Operating system, version and so on)

Win 11 and Win 10 on any* version

.NET Framework version

UWP x64 app (Target: win11 22H2, Min: win10 1903), c# 9.0

Description

IndexOutOfRangeException is thrown when running SaveAsWebpAsync on a published UWP app, independently of which image is loaded into the stream, any image will fail, any format will fail. The same code runs fine when running the UWP app directly from Visual Studio, either Debug or Release.

 IndexOutOfRangeException: ImageCreation/Create > Failure while creating image
  -- IndexOutOfRangeException, "Index was outside the bounds of the array."
   at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowIndexOutOfRangeException() + 0x25
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoding.ITransformOne(Span`1, Span`1, Span`1, Span`1) + 0x422
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoding.ITransformTwo(Span`1, Span`1, Span`1, Span`1) + 0x93
   at SixLabors.ImageSharp.Formats.Webp.Lossy.QuantEnc.ReconstructIntra16(Vp8EncIterator, Vp8SegmentInfo, Vp8ModeScore, Span`1, Int32) + 0x865
   at SixLabors.ImageSharp.Formats.Webp.Lossy.QuantEnc.PickBestIntra16(Vp8EncIterator, Vp8ModeScore&, Vp8SegmentInfo[], Vp8EncProba) + 0x828
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.Decimate(Vp8EncIterator, Vp8ModeScore&, Vp8RdLevel) + 0x65
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.OneStatPass(Int32, Int32, Int32, Int32, Vp8RdLevel, Int32, PassStats) + 0x560
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.StatLoop(Int32, Int32, Int32, Int32) + 0x145
   at SixLabors.ImageSharp.Formats.Webp.Lossy.Vp8Encoder.Encode[TPixel](Image`1, Stream) + 0xa33
   at SixLabors.ImageSharp.Formats.Webp.WebpEncoderCore.Encode[TPixel](Image`1, Stream, CancellationToken) + 0x1b9
   at SixLabors.ImageSharp.Formats.ImageEncoderUtilities.<EncodeAsync>g__DoEncodeAsync|0_0[TPixel](Stream, ImageEncoderUtilities.<>c__DisplayClass0_0`1&) + 0x67
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x21
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0x7e
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x53
   at SixLabors.ImageSharp.Formats.ImageEncoderUtilities.<EncodeAsync>d__0`1.MoveNext() + 0x37e
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x21
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0x7e
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task) + 0x53
   at App.Images.ImageCreation.<Create>d__1.MoveNext() + 0x185

Steps to Reproduce

var outputStream = new MemoryStream();
using (var image = await Image.LoadAsync(stream))
{
     image.Mutate(img => img.Resize(Size, Size));
     await image.SaveAsWebpAsync(outputStream); // Exception here
}

The code work fine when running the app from Visual Studio, either from Debug or Release modes.

But it will fail only if:

  • I publish it to the MS Store, install from the store and run.
  • I try to publish a sideload package from VS choosing Release type, install this package and run.

If I try to create a sample app to try demostrate the issue, it works, although the code is being tested is virtually the same.

Images

Any image, any format.

brunomuratore avatar Oct 20 '23 16:10 brunomuratore

Hi @brunomuratore

Normally we wouldn't support issues raised against previous major versions (we're on v3 now) but I'm not sure what target frameworks UWP support.

The issue itself is not an ImageSharp one but rather appears to me to be a bug in the compiler.

@Sergio0694 Apologies for the tag but you know a lot more about UWP than me. What are your thoughts?

JimBobSquarePants avatar Oct 23 '23 23:10 JimBobSquarePants

"I'm not sure what target frameworks UWP support"

UWP is picking up the .NET Standard 2.0 target (it uses the uap TFM, which is a weird ~.NET Core 2.1 TFM, but with all Span<T> APIs missing from its ref assembly... It's a whole thing 😅), long story short it only accepts either code targeting uap specifically, or otherwise any .NET Standard <= 2.0 TFM.

"The issue itself is not an ImageSharp one but rather appears to me to be a bug in the compiler."

Yeah this is a weird one, especially because it also repros with sideloaded Release packages (meaning ones you compiled locally), yet it doesn't with a normal Release build from VS. Impossible to say exactly what's causing that without just debugging the app. I do remember using ImageSharp on UWP in the past, and it worked (my app Legere is using 1.0.0-beta0010).

...Honestly I think your best bet for UWP right now is try downgrading the ImageSharp package step by step until you find a previous release that happens to work 🙃

Sergio0694 avatar Oct 24 '23 00:10 Sergio0694

Thanks @Sergio0694 appreciate it!!

@brunomuratore when you say the following

Any image, any format.

I'm assuming you mean input images. Does saving as other image formats (jpg, png, etc) work?

JimBobSquarePants avatar Oct 24 '23 00:10 JimBobSquarePants

Shouldn't UWP users be migrating to WinUI 3?

JimBobSquarePants avatar Oct 24 '23 04:10 JimBobSquarePants