public async Task <bool> WriteAndCropFile(string fileHash, OffsetModel offsetData, int sourceWidth, int sourceHeight, FileIndexItem.Rotation rotation, string reference = null) { try { using (var thumbnailStream = new MemoryStream(offsetData.Data, offsetData.Index, offsetData.Count)) using (var smallImage = await Image.LoadAsync(thumbnailStream)) using (var outputStream = new MemoryStream()) { var smallImageWidth = smallImage.Width; var smallImageHeight = smallImage.Height; var result = NewImageSize.NewImageSizeCalc(smallImageWidth, smallImageHeight, sourceWidth, sourceHeight); smallImage.Mutate( i => i.Resize(smallImageWidth, smallImageHeight, KnownResamplers.Lanczos3) .Crop(new Rectangle(result.DestX, result.DestY, result.DestWidth, result.DestHeight))); var larger = (int)Math.Round(result.DestWidth * 1.2, 0); smallImage.Mutate( i => i.Resize(larger, 0, KnownResamplers.Lanczos3)); var rotate = RotateEnumToDegrees(rotation); smallImage.Mutate( i => i.Rotate(rotate)); await smallImage.SaveAsJpegAsync(outputStream); await _thumbnailStorage.WriteStreamAsync(outputStream, ThumbnailNameHelper.Combine(fileHash, ThumbnailSize.TinyMeta)); if (_appSettings.ApplicationType == AppSettings.StarskyAppType.WebController) { _logger.LogInformation($"[WriteAndCropFile] fileHash: {fileHash} is written"); } } return(true); } catch (Exception ex) { var message = ex.Message; if (message.StartsWith("Image cannot be loaded")) { message = "Image cannot be loaded"; } _logger.LogError($"[WriteFile@meta] Exception {reference} {message}", ex); return(false); } }
public void NewImageSize_Portrait() { var sourceWidth = 2832; var sourceHeight = 4240; var smallWidth = 120; var smallHeight = 160; var rNewImageSizeCalc = NewImageSize.NewImageSizeCalc(smallWidth, smallHeight, sourceWidth, sourceHeight); Assert.AreEqual(160, rNewImageSizeCalc.DestHeight); Assert.AreEqual(106, rNewImageSizeCalc.DestWidth); Assert.AreEqual(7, rNewImageSizeCalc.DestX); Assert.AreEqual(0, rNewImageSizeCalc.DestY); }
/// <summary> /// 指定大小简单等比缩放图片 /// </summary> /// <param name="original_image"></param> /// <param name="new_width"></param> /// <returns></returns> //public static Bitmap GetSimpleThumbnailImage(Image original_image, int new_width, int new_height) //{ // NewImageSize imageSize = new NewImageSize(original_image.Width, original_image.Height, new_width, new_height); // Bitmap returnImage = null; // returnImage = new Bitmap(original_image, imageSize.NewWidth, imageSize.NewHeight); // return returnImage; //} /// <summary> /// 根据指定大小绘制高质量缩略图 /// </summary> /// <param name="original_image"></param> /// <param name="new_width"></param> /// <returns></returns> public static Bitmap GetThumbnailImage(Image original_image, ThumbnailImageType titCase, int target_width, int target_height) { System.Drawing.Graphics graphic = null; Bitmap returnImage = null; int origin_width = original_image.Width; int origin_height = original_image.Height; int paste_x = 0, paste_y = 0; NewImageSize imageSize = new NewImageSize(original_image.Width, original_image.Height, target_width, target_height); int new_width = imageSize.NewWidth, new_height = imageSize.NewHeight; target_width = imageSize.NewWidth; target_height = imageSize.NewHeight; float origin_ratio = imageSize.OrignRatio; float target_ratio = imageSize.NewRatio; //缩略图生成方式 switch (titCase) { //缩放全图到正确比例 case ThumbnailImageType.Zoom: if (target_ratio > origin_ratio) { //较宽图片 new_height = (int)Math.Floor((float)target_width / origin_ratio); new_width = target_width; } else { //较高图片 new_height = target_height; new_width = (int)Math.Floor(origin_ratio * (float)target_height); } new_width = new_width > target_width ? target_width : new_width; new_height = new_height > target_height ? target_height : new_height; break; //截取高宽比中间部分 case ThumbnailImageType.Cut: if (target_ratio > origin_ratio) { //较高图片 new_height = (int)Math.Floor((float)target_width / origin_ratio); new_width = target_width; } else { //较宽图片 new_height = target_height; new_width = (int)Math.Floor(origin_ratio * (float)target_height); } break; } paste_x = (target_width - new_width) / 2; paste_y = (target_height - new_height) / 2; // 创建缩略图 returnImage = new System.Drawing.Bitmap(target_width, target_height); graphic = System.Drawing.Graphics.FromImage(returnImage); //指定画布大小并以透明色填充空余部分 graphic.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), new System.Drawing.Rectangle(0, 0, target_width, target_height)); //设置高质量绘画 graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; graphic.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //输出缩略图 graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height); return(returnImage); }