@@ -72,7 +72,7 @@ public static (Size, Rectangle) CalculateTargetLocationAndBounds(Size sourceSize
7272
7373 // case ResizeMode.Stretch:
7474 default :
75- return ( new Size ( width , height ) , new Rectangle ( 0 , 0 , width , height ) ) ;
75+ return ( new Size ( Sanitize ( width ) , Sanitize ( height ) ) , new Rectangle ( 0 , 0 , Sanitize ( width ) , Sanitize ( height ) ) ) ;
7676 }
7777 }
7878
@@ -143,7 +143,7 @@ private static (Size, Rectangle) CalculateBoxPadRectangle(
143143 }
144144
145145 // Target image width and height can be different to the rectangle width and height.
146- return ( new Size ( width , height ) , new Rectangle ( targetX , targetY , targetWidth , targetHeight ) ) ;
146+ return ( new Size ( Sanitize ( width ) , Sanitize ( height ) ) , new Rectangle ( targetX , targetY , Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) ) ;
147147 }
148148
149149 // Switch to pad mode to downscale and calculate from there.
@@ -253,7 +253,7 @@ private static (Size, Rectangle) CalculateCropRectangle(
253253 }
254254
255255 // Target image width and height can be different to the rectangle width and height.
256- return ( new Size ( width , height ) , new Rectangle ( targetX , targetY , targetWidth , targetHeight ) ) ;
256+ return ( new Size ( Sanitize ( width ) , Sanitize ( height ) ) , new Rectangle ( targetX , targetY , Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) ) ;
257257 }
258258
259259 private static ( Size , Rectangle ) CalculateMaxRectangle (
@@ -282,7 +282,7 @@ private static (Size, Rectangle) CalculateMaxRectangle(
282282 }
283283
284284 // Replace the size to match the rectangle.
285- return ( new Size ( targetWidth , targetHeight ) , new Rectangle ( 0 , 0 , targetWidth , targetHeight ) ) ;
285+ return ( new Size ( Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) , new Rectangle ( 0 , 0 , Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) ) ;
286286 }
287287
288288 private static ( Size , Rectangle ) CalculateMinRectangle (
@@ -330,7 +330,7 @@ private static (Size, Rectangle) CalculateMinRectangle(
330330 }
331331
332332 // Replace the size to match the rectangle.
333- return ( new Size ( targetWidth , targetHeight ) , new Rectangle ( 0 , 0 , targetWidth , targetHeight ) ) ;
333+ return ( new Size ( Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) , new Rectangle ( 0 , 0 , Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) ) ;
334334 }
335335
336336 private static ( Size , Rectangle ) CalculatePadRectangle (
@@ -398,7 +398,7 @@ private static (Size, Rectangle) CalculatePadRectangle(
398398 }
399399
400400 // Target image width and height can be different to the rectangle width and height.
401- return ( new Size ( width , height ) , new Rectangle ( targetX , targetY , targetWidth , targetHeight ) ) ;
401+ return ( new Size ( Sanitize ( width ) , Sanitize ( height ) ) , new Rectangle ( targetX , targetY , Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) ) ;
402402 }
403403
404404 private static ( Size , Rectangle ) CalculateManualRectangle (
@@ -419,9 +419,11 @@ private static (Size, Rectangle) CalculateManualRectangle(
419419 int targetHeight = targetRectangle . Height > 0 ? targetRectangle . Height : height ;
420420
421421 // Target image width and height can be different to the rectangle width and height.
422- return ( new Size ( width , height ) , new Rectangle ( targetX , targetY , targetWidth , targetHeight ) ) ;
422+ return ( new Size ( Sanitize ( width ) , Sanitize ( height ) ) , new Rectangle ( targetX , targetY , Sanitize ( targetWidth ) , Sanitize ( targetHeight ) ) ) ;
423423 }
424424
425425 private static void ThrowInvalid ( string message ) => throw new InvalidOperationException ( message ) ;
426+
427+ private static int Sanitize ( int input ) => Math . Max ( 1 , input ) ;
426428 }
427429}
0 commit comments