DEBUG and RELEASE modeI made a mistake when cloning and cropping an image.
The problem is that the exception thrown was quite obscure. The error was related to MaxDegreeOfParallelism being invalid. Here is the stack trace:
SixLabors.ImageSharp.ImageProcessingException: An error occurred when processing the image using DrawImageProcessor`2. See the inner exception for more detail. ---> SixLabors.ImageSharp.ImageProcessingException: An error occurred when processing the image using DrawImageProcessor`2. See the inner exception for more detail. ---> System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: MaxDegreeOfParallelism
at System.Threading.Tasks.ParallelOptions.set_MaxDegreeOfParallelism(Int32 value)
at SixLabors.ImageSharp.ParallelUtils.ParallelHelper.IterateRows(Rectangle rectangle, ParallelExecutionSettings& parallelSettings, Action`1 body)
at SixLabors.ImageSharp.Processing.Processors.Drawing.DrawImageProcessor`2.OnFrameApply(ImageFrame`1 source, Rectangle sourceRectangle, Configuration configuration)
at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(ImageFrame`1 source, Rectangle sourceRectangle, Configuration configuration)
--- End of inner exception stack trace ---
at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(ImageFrame`1 source, Rectangle sourceRectangle, Configuration configuration)
at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(Image`1 source, Rectangle sourceRectangle)
--- End of inner exception stack trace ---
at SixLabors.ImageSharp.Processing.Processors.ImageProcessor`1.Apply(Image`1 source, Rectangle sourceRectangle)
at SixLabors.ImageSharp.Processing.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor, Rectangle rectangle)
at SixLabors.ImageSharp.Processing.DefaultInternalImageProcessorContext`1.ApplyProcessor(IImageProcessor`1 processor)
at SixLabors.ImageSharp.Processing.DrawImageExtensions.DrawImage[TPixelDst,TPixelSrc](IImageProcessingContext`1 source, Image`1 image, Point location, GraphicsOptions options)
I've tracked down the root cause to:
https://github.com/SixLabors/ImageSharp/blob/2849fd9c1dc19a7338235f6c33f4970e5a12dee3/src/ImageSharp/Common/ParallelUtils/ParallelHelper.cs#L50-L64
If the rectangle height is negative, then maxSteps will be negative, which will then set the MaxDegreeOfParallelism to a negative value.
It took me ages to work out what happened! I got my top and left variables back to front.
My crop was:
image.Mutate(x => x.DrawImage(source, new Point(top, left), GraphicsOptions.Default));
And should have been:
image.Mutate(x => x.DrawImage(source, new Point(left, top), GraphicsOptions.Default));
I think the ideal solution here is a couple more guards to provide a better error message when someone (like me) does something silly and messes up their coordinates.
Well discovered!
This is the kind of simple fix that would be perfect for a first time PR if you're up for that.
Yeah, I'll give it a go.
Most helpful comment
Yeah, I'll give it a go.