@ -63,7 +63,7 @@ public class SkiaSharpImageResizerContributor : IImageResizerContributor, ITrans
return new ImageResizeResult < Stream > ( stream , ImageProcessState . Unsupported ) ;
}
using var resized = original . Resize ( new SKImageInfo ( ( int ) resizeArgs . Width , ( int ) resizeArgs . Height ) , Options . SKSamplingOption s) ;
using var resized = ApplyResize ( original , resizeArg s) ;
using var image = SKImage . FromBitmap ( resized ) ;
var memoryStream = new MemoryStream ( ) ;
@ -139,4 +139,142 @@ public class SkiaSharpImageResizerContributor : IImageResizerContributor, ITrans
_ = > false
} ;
}
protected virtual SKBitmap ApplyResize ( SKBitmap source , ImageResizeArgs resizeArgs )
{
var targetWidth = ( int ) resizeArgs . Width ;
var targetHeight = ( int ) resizeArgs . Height ;
if ( targetWidth < = 0 & & targetHeight < = 0 )
{
return source . Copy ( ) ;
}
if ( targetWidth < = 0 )
{
targetWidth = Math . Max ( 1 , ( int ) Math . Round ( ( double ) source . Width * targetHeight / source . Height ) ) ;
}
else if ( targetHeight < = 0 )
{
targetHeight = Math . Max ( 1 , ( int ) Math . Round ( ( double ) source . Height * targetWidth / source . Width ) ) ;
}
var mode = resizeArgs . Mode = = ImageResizeMode . Default ? ImageResizeMode . Crop : resizeArgs . Mode ;
if ( mode = = ImageResizeMode . None )
{
mode = ImageResizeMode . Crop ;
}
switch ( mode )
{
case ImageResizeMode . Stretch :
return source . Resize ( new SKImageInfo ( targetWidth , targetHeight ) , Options . SKSamplingOptions ) ;
case ImageResizeMode . Max :
{
var scale = Math . Min ( ( double ) targetWidth / source . Width , ( double ) targetHeight / source . Height ) ;
var newW = Math . Max ( 1 , ( int ) Math . Round ( source . Width * scale ) ) ;
var newH = Math . Max ( 1 , ( int ) Math . Round ( source . Height * scale ) ) ;
return source . Resize ( new SKImageInfo ( newW , newH ) , Options . SKSamplingOptions ) ;
}
case ImageResizeMode . Min :
{
var scale = Math . Max ( ( double ) targetWidth / source . Width , ( double ) targetHeight / source . Height ) ;
var newW = Math . Max ( 1 , ( int ) Math . Round ( source . Width * scale ) ) ;
var newH = Math . Max ( 1 , ( int ) Math . Round ( source . Height * scale ) ) ;
return source . Resize ( new SKImageInfo ( newW , newH ) , Options . SKSamplingOptions ) ;
}
case ImageResizeMode . Crop :
{
var scale = Math . Max ( ( double ) targetWidth / source . Width , ( double ) targetHeight / source . Height ) ;
var intermediateW = Math . Max ( 1 , ( int ) Math . Round ( source . Width * scale ) ) ;
var intermediateH = Math . Max ( 1 , ( int ) Math . Round ( source . Height * scale ) ) ;
using var intermediate = source . Resize ( new SKImageInfo ( intermediateW , intermediateH ) , Options . SKSamplingOptions ) ;
var bitmap = new SKBitmap ( targetWidth , targetHeight ) ;
try
{
using var canvas = new SKCanvas ( bitmap ) ;
var srcX = ( intermediateW - targetWidth ) / 2 ;
var srcY = ( intermediateH - targetHeight ) / 2 ;
canvas . DrawBitmap (
intermediate ,
new SKRect ( srcX , srcY , srcX + targetWidth , srcY + targetHeight ) ,
new SKRect ( 0 , 0 , targetWidth , targetHeight ) ) ;
return bitmap ;
}
catch
{
bitmap . Dispose ( ) ;
throw ;
}
}
case ImageResizeMode . Pad :
{
var scale = Math . Min ( ( double ) targetWidth / source . Width , ( double ) targetHeight / source . Height ) ;
var intermediateW = Math . Max ( 1 , ( int ) Math . Round ( source . Width * scale ) ) ;
var intermediateH = Math . Max ( 1 , ( int ) Math . Round ( source . Height * scale ) ) ;
using var intermediate = source . Resize ( new SKImageInfo ( intermediateW , intermediateH ) , Options . SKSamplingOptions ) ;
var bitmap = new SKBitmap ( targetWidth , targetHeight ) ;
try
{
using var canvas = new SKCanvas ( bitmap ) ;
canvas . Clear ( SKColors . Transparent ) ;
var dstX = ( targetWidth - intermediateW ) / 2 ;
var dstY = ( targetHeight - intermediateH ) / 2 ;
canvas . DrawBitmap ( intermediate , new SKPoint ( dstX , dstY ) ) ;
return bitmap ;
}
catch
{
bitmap . Dispose ( ) ;
throw ;
}
}
case ImageResizeMode . BoxPad :
{
SKBitmap ? scaled = null ;
try
{
var working = source ;
if ( source . Width > targetWidth | | source . Height > targetHeight )
{
var scale = Math . Min ( ( double ) targetWidth / source . Width , ( double ) targetHeight / source . Height ) ;
var newW = Math . Max ( 1 , ( int ) Math . Round ( source . Width * scale ) ) ;
var newH = Math . Max ( 1 , ( int ) Math . Round ( source . Height * scale ) ) ;
scaled = source . Resize ( new SKImageInfo ( newW , newH ) , Options . SKSamplingOptions ) ;
working = scaled ;
}
var bitmap = new SKBitmap ( targetWidth , targetHeight ) ;
try
{
using var canvas = new SKCanvas ( bitmap ) ;
canvas . Clear ( SKColors . Transparent ) ;
var dstX = ( targetWidth - working . Width ) / 2 ;
var dstY = ( targetHeight - working . Height ) / 2 ;
canvas . DrawBitmap ( working , new SKPoint ( dstX , dstY ) ) ;
return bitmap ;
}
catch
{
bitmap . Dispose ( ) ;
throw ;
}
}
finally
{
scaled ? . Dispose ( ) ;
}
}
default :
throw new NotSupportedException ( "Resize mode " + resizeArgs . Mode + " is not supported!" ) ;
}
}
}