public static Boolean saveAvatarPrivate(int x, int y, String srcPath, ThumbnailType ttype) { String thumbPath = Img.GetThumbPath(srcPath, ttype); try { using (Image img = Image.FromFile(srcPath)) { if (img.Size.Width <= x && img.Size.Height <= y) { File.Copy(srcPath, thumbPath); } else if (img.RawFormat.Equals(System.Drawing.Imaging.ImageFormat.Gif) && ImageAnimator.CanAnimate(img)) { File.Copy(srcPath, thumbPath); } else { logger.Info("save thumbnail..." + ttype.ToString() + ": " + srcPath + "=>" + thumbPath); Img.SaveThumbnail(srcPath, thumbPath, x, y, SaveThumbnailMode.Cut); } return(true); } } catch (OutOfMemoryException ex) { logger.Error("file format error: " + srcPath); return(false); } }
public static Boolean saveAvatarPrivate( int x, int y, String srcPath, ThumbnailType ttype ) { String thumbPath = Img.GetThumbPath( srcPath, ttype ); try { using (Image img = Image.FromFile( srcPath )) { if (img.Size.Width <= x && img.Size.Height <= y) { File.Copy( srcPath, thumbPath ); } else if (img.RawFormat.Equals( System.Drawing.Imaging.ImageFormat.Gif ) && ImageAnimator.CanAnimate( img )) { File.Copy( srcPath, thumbPath ); } else { logger.Info( "save thumbnail..." + ttype.ToString() + ": " + srcPath + "=>" + thumbPath ); Img.SaveThumbnail( srcPath, thumbPath, x, y, SaveThumbnailMode.Cut ); } return true; } } catch (OutOfMemoryException ex) { logger.Error( "file format error: " + srcPath ); return false; } }
public string GetThumbnail(string thumbnail, ThumbnailType type) { var possible = Path.Combine(_thumbnailCacheDirectory, type.ToString(), thumbnail); if (File.Exists(possible)) return possible; // TODO: global sync lock? using (var imageStream = _postThumbnailService.GetImage(thumbnail)) { switch (type) { case ThumbnailType.Full: using (var fileStream = File.OpenWrite(possible)) imageStream.CopyTo(fileStream); break; case ThumbnailType.Post: using (var image = Image.FromStream(imageStream)) { var instructions = new Instructions("width=70&height=70&scale=both") { Width = 70, Height = 70, Scale = ScaleMode.Both }; // we want horizontally long images to have no padding. simply render the images with a max width. // for vertically long images, we want to fill the entire canvas area with no black space on the sides. var ratio = image.Width / (double)image.Height; if (ratio >= 1.5) { // the image is REALLY wide, so, let's just fill, cropping the ends. instructions.Mode = FitMode.Crop; } else if (ratio >= 1) { // the image is wide instructions.Mode = FitMode.Max; } else { // the image is tall instructions.Mode = FitMode.Crop; } ImageBuilder.Current.Build(new ImageJob() { Source = image, Dest = possible, Instructions = instructions }); } break; default: throw new Exception("unknown type"); } } return possible; }
public string GetThumbnail(string thumbnail, ThumbnailType type) { var possible = Path.Combine(_thumbnailCacheDirectory, type.ToString(), thumbnail); if (File.Exists(possible)) { return(possible); } // TODO: global sync lock? using (var imageStream = _postThumbnailService.GetImage(thumbnail)) { switch (type) { case ThumbnailType.Full: using (var fileStream = File.OpenWrite(possible)) imageStream.CopyTo(fileStream); break; case ThumbnailType.Post: using (var image = Image.FromStream(imageStream)) { var instructions = new Instructions("width=70&height=70&scale=both") { Width = 70, Height = 70, Scale = ScaleMode.Both }; // we want horizontally long images to have no padding. simply render the images with a max width. // for vertically long images, we want to fill the entire canvas area with no black space on the sides. var ratio = image.Width / (double)image.Height; if (ratio >= 1.5) { // the image is REALLY wide, so, let's just fill, cropping the ends. instructions.Mode = FitMode.Crop; } else if (ratio >= 1) { // the image is wide instructions.Mode = FitMode.Max; } else { // the image is tall instructions.Mode = FitMode.Crop; } ImageBuilder.Current.Build(new ImageJob() { Source = image, Dest = possible, Instructions = instructions }); } break; default: throw new Exception("unknown type"); } } return(possible); }