SixLabors/ImageSharp

Bug with negative background location while drawing an image

tocsoft opened this issue · 1 comments

We incorrectly calculate the updated area of interest of the foreground image when applying a negative offset to the background drawing location.

I checked the source code and I think I see the issue around these lines:

foregroundRectangle.Width += this.BackgroundLocation.X;

The foregroundRectangle.Width is adjusted by the background location X value if it was negative. This is resulting in cropping off the right side of the top image, not the left. Same for the Y direction just below that.

I created a test project to demonstrate the difference to make sure I wasn't going crazy, but here is an illustration:

imagesharp-3 0 2 Above: 3.0.2 behavior. The top/left of the top image is lost (expected due to negative coordinates).

imagesharp-3 1-intended
Above: what I believe the code in 3.1 is intended to do. The output would remain the same. I believe adjusting the rectangle's X an Y (in addition to removing it from the Width and Height) could have this effect. I am not an expert at what's happening below there, though, so I could be wrong.

imagesharp-3 1-actual
Above: what is actually happening. The top image is indeed painted in the same box, but it originates from the top left corner and cuts off the bottom/right of the image instead of the top/left.

Thanks again for your time!

Originally posted by @aceoft in #2603 (comment)

fix has been merged