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

SaveAsWebpAsync throws IndexOutOfRangeException in UWP published app #2567

Open
4 tasks done
brunomuratore opened this issue Oct 20, 2023 · 4 comments
Open
4 tasks done
Labels
upstream-issue Issue depends on upstream dotnet fix.

Comments

@brunomuratore
Copy link

brunomuratore commented Oct 20, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have verified that I am running the latest version of ImageSharp
  • I have verified if the problem exist in both DEBUG and RELEASE mode
  • 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.

@JimBobSquarePants
Copy link
Member

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?

@Sergio0694
Copy link
Member

Sergio0694 commented Oct 24, 2023

"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 🙃

@JimBobSquarePants
Copy link
Member

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
Copy link
Member

Shouldn't UWP users be migrating to WinUI 3?

@JimBobSquarePants JimBobSquarePants added upstream-issue Issue depends on upstream dotnet fix. and removed needs triage labels Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream-issue Issue depends on upstream dotnet fix.
Projects
None yet
Development

No branches or pull requests

3 participants