diff --git a/src/ImageSharp/Formats/AnimationUtilities.cs b/src/ImageSharp/Formats/AnimationUtilities.cs index b66efd7f5..a3a5292a2 100644 --- a/src/ImageSharp/Formats/AnimationUtilities.cs +++ b/src/ImageSharp/Formats/AnimationUtilities.cs @@ -148,11 +148,12 @@ internal static class AnimationUtilities PixelOperations.Instance.FromVector4Destructive(configuration, result, resultFrame.DangerousGetPixelRowMemory(y).Span, PixelConversionModifiers.Scale); } - Rectangle bounds = Rectangle.FromLTRB( - left = Numerics.Clamp(left, 0, resultFrame.Width - 1), - top = Numerics.Clamp(top, 0, resultFrame.Height - 1), - Numerics.Clamp(right, left + 1, resultFrame.Width), - Numerics.Clamp(bottom, top + 1, resultFrame.Height)); + left = Math.Max(0, Math.Min(left, resultFrame.Width - 1)); + top = Math.Max(0, Math.Min(top, resultFrame.Height - 1)); + right = Math.Max(left + 1, Math.Min(right, resultFrame.Width)); + bottom = Math.Max(top + 1, Math.Min(bottom, resultFrame.Height)); + + Rectangle bounds = Rectangle.FromLTRB(left, top, right, bottom); // Webp requires even bounds if (clampingMode == ClampingMode.Even)