public void MakeThumbnail() { var file = "test2.jpg"; MagickWandEnvironment.Genesis(); using(var mw = new MagickWand()) { mw.ReadImage("test.jpg"); mw.ScaleImage(120, 100); mw.WriteImage(file, true); Assert.True(File.Exists(file), "scaled image not created"); } using(var mw = new MagickWand(file)) { Assert.True(mw.ImageWidth == 120, "width does not match the expected size"); Assert.True(mw.ImageHeight == 100, "height does not match the expected size"); } File.Delete(file); MagickWandEnvironment.Terminus(); }
void RegenerateVideoThumbnail(string sourceFile, string destThumb) { DumpImageFromVideo(sourceFile, destThumb); var width = 240; var height = 160; using (var wand = new MagickWand(destThumb)) { float idealAspect = (float)width / (float)height; float actualAspect = (float)wand.ImageWidth / (float)wand.ImageHeight; if (idealAspect >= actualAspect) { width = (int)(actualAspect * (float)height); } else { height = (int)((float)width / actualAspect); } wand.ScaleImage((uint)width, (uint)height); // sharpen after potentially resizing // http://www.imagemagick.org/Usage/resize/#resize_unsharp wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008); wand.WriteImage(destThumb, true); } }
void GenerateThumbSq(Ffmpeg ffmpeg, string localSourceFile, string localThumbSqFile, MovieMetadata mm) { ffmpeg.ExtractFrame(localSourceFile, localThumbSqFile, GetThumbnailSeconds(mm)); using (var wand = new MagickWand(localThumbSqFile)) { var width = (double)wand.ImageWidth; var height = (double)wand.ImageHeight; var aspect = width / height; if (aspect >= THUMB_SQ_ASPECT) { var newWidth = (width / height) * THUMB_SQ_HEIGHT; // scale image to final height wand.ScaleImage((uint)newWidth, THUMB_SQ_HEIGHT); // crop sides as needed wand.CropImage(THUMB_SQ_WIDTH, THUMB_SQ_HEIGHT, (int)(newWidth - THUMB_SQ_WIDTH) / 2, 0); } else { var newHeight = THUMB_SQ_WIDTH / (width / height); // scale image to final width wand.ScaleImage(THUMB_SQ_WIDTH, (uint)newHeight); // crop top and bottom as needed wand.CropImage(THUMB_SQ_WIDTH, THUMB_SQ_HEIGHT, 0, (int)(newHeight - THUMB_SQ_HEIGHT) / 2); } // sharpen after potentially resizing // http://www.imagemagick.org/Usage/resize/#resize_unsharp wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008); wand.WriteImage(localThumbSqFile, true); mm.ThumbSqHeight = THUMB_SQ_HEIGHT; mm.ThumbSqWidth = THUMB_SQ_WIDTH; } }
public void Generate() { using (var wand = new MagickWand(_sourcePath)) { var width = (double)wand.ImageWidth; var height = (double)wand.ImageHeight; var aspect = width / height; if (aspect >= Aspect) { var newWidth = (width / height) * FinalHeight; // scale image to final height wand.ScaleImage((uint)newWidth, FinalHeight); // crop sides as needed wand.CropImage(FinalWidth, FinalHeight, (int)(newWidth - FinalWidth) / 2, 0); } else { var newHeight = FinalWidth / (width / height); // scale image to final width wand.ScaleImage(FinalWidth, (uint)newHeight); // crop top and bottom as needed wand.CropImage(FinalWidth, FinalHeight, 0, (int)(newHeight - FinalHeight) / 2); } // sharpen after potentially resizing // http://www.imagemagick.org/Usage/resize/#resize_unsharp wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008); wand.WriteImage(_destPath, true); } ExecuteJpegOptimAsync(_destPath); ExecuteJpegTranAsync(_destPath); }
void GenerateThumbnail(Ffmpeg ffmpeg, string localSourceFile, string localThumbnailFile, MovieMetadata mm) { ffmpeg.ExtractFrame(localSourceFile, localThumbnailFile, GetThumbnailSeconds(mm)); using (var wand = new MagickWand(localThumbnailFile)) { wand.GetLargestDimensionsKeepingAspectRatio(THUMB_WIDTH, THUMB_HEIGHT, out uint width, out uint height); wand.ScaleImage(width, height); // sharpen after potentially resizing // http://www.imagemagick.org/Usage/resize/#resize_unsharp wand.UnsharpMaskImage(0, 0.7, 0.7, 0.008); wand.WriteImage(localThumbnailFile, true); mm.ThumbHeight = (int)height; mm.ThumbWidth = (int)width; } }