Skip to content

Commit 7ab7b22

Browse files
committed
handle when foreground overhangs bottom of background
1 parent cb07e36 commit 7ab7b22

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

src/ImageSharp/Processing/Processors/Drawing/DrawImageProcessor{TPixelBg,TPixelFg}.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ protected override void OnFrameApply(ImageFrame<TPixelBg> source)
9696
top = 0;
9797
}
9898

99+
if (left + foregroundRectangle.Width > source.Width)
100+
{
101+
// will overhange, lets trim it down
102+
int diff = (left + foregroundRectangle.Width) - source.Width;
103+
foregroundRectangle.Width -= diff;
104+
}
105+
106+
if (top + foregroundRectangle.Height > source.Height)
107+
{
108+
// will overhange, lets trim it down
109+
int diff = (top + foregroundRectangle.Height) - source.Height;
110+
foregroundRectangle.Height -= diff;
111+
}
112+
99113
int width = foregroundRectangle.Width;
100114
int height = foregroundRectangle.Height;
101115
if (width <= 0 || height <= 0)

tests/ImageSharp.Tests/Drawing/DrawImageTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,25 @@ public void Issue2447_C<TPixel>(TestImageProvider<TPixel> provider)
251251
appendPixelTypeToFileName: false,
252252
appendSourceFileOrDescription: false);
253253
}
254+
255+
[Theory]
256+
[WithFile(TestImages.Png.Issue2447, PixelTypes.Rgba32)]
257+
public void Issue2603<TPixel>(TestImageProvider<TPixel> provider)
258+
where TPixel : unmanaged, IPixel<TPixel>
259+
{
260+
using Image<TPixel> foreground = provider.GetImage();
261+
using Image<Rgba32> background = new(100, 100, new Rgba32(0, 255, 255));
262+
263+
background.Mutate(c => c.DrawImage(foreground, new Point(80, 80), new Rectangle(32, 32, 32, 32), 1F));
264+
265+
background.DebugSave(
266+
provider,
267+
appendPixelTypeToFileName: false,
268+
appendSourceFileOrDescription: false);
269+
270+
background.CompareToReferenceOutput(
271+
provider,
272+
appendPixelTypeToFileName: false,
273+
appendSourceFileOrDescription: false);
274+
}
254275
}
Lines changed: 3 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)