public static void ApplyPreviewImageMagick(string source, string destination, double maxWidth = 0d, double maxHeight = 0d) {
using (var image = new MagickImage(source)) {
if (maxWidth > 0d || maxHeight > 0d) {
var k = Math.Max(maxHeight / image.Height, maxWidth / image.Width);
image.Interpolate = PixelInterpolateMethod.Bicubic;
image.FilterType = FilterType.Lanczos;
image.Sharpen();
image.Resize((int)(k * image.Width), (int)(k * image.Height));
image.Crop(CommonAcConsts.PreviewWidth, CommonAcConsts.PreviewHeight, Gravity.Center);
}
image.Quality = 95;
image.Density = new MagickGeometry(96, 96);
if (File.Exists(destination)) {
try {
File.Delete(destination);
} catch (UnauthorizedAccessException) {
Thread.Sleep(200);
File.Delete(destination);
}
}
image.Write(destination);
}
}