public ImageSize Resize(ImageSize originalSize, ImageSize targetSize) { var aspectRatio = (float)originalSize.Width / (float)originalSize.Height; var width = targetSize.Width; var height = targetSize.Height; if (originalSize.Width > targetSize.Width || originalSize.Height > targetSize.Height) { if (aspectRatio > 1) { height = (int)(targetSize.Height / aspectRatio); } else { width = (int)(targetSize.Width * aspectRatio); } } else { width = originalSize.Width; height = originalSize.Height; } return new ImageSize { Width = Math.Max(width, 1), Height = Math.Max(height, 1) }; }
public byte[] Create(Stream source, ImageSize desiredSize, string contentType) { using (var image = Image.FromStream(source)) { var originalSize = new ImageSize { Height = image.Height, Width = image.Width }; var size = resizer.Resize(originalSize, desiredSize); using (var thumbnail = new Bitmap(size.Width, size.Height)) { ScaleImage(image, thumbnail); using (var memoryStream = new MemoryStream()) { thumbnail.Save(memoryStream, ImageFormats[contentType]); return memoryStream.ToArray(); } } } }
/// <summary> /// Retourne la miniature d'une image stockée dans la BDD sous forme de média /// </summary> /// <param name="path">L'identifiant du média</param> /// <returns>Instance d'un FileContentResult contenant la miniature</returns> public ActionResult Thumbnail(string path) { Guid l_mediaId = new Guid(path); Media l_media = Media.LoadById(l_mediaId); FileContentResult l_fileContentResult; IThumbnailCreator l_thumbnailCreator = DI.Current.Resolve<IThumbnailCreator>(); using (Stream l_stream = l_media.Fichier.GetInputStream()) { ImageSize l_imageSize = new ImageSize(); l_imageSize.Width = 80; l_imageSize.Height = 80; ImageSize imageSize1 = l_imageSize; l_fileContentResult = base.File(l_thumbnailCreator.Create(l_stream, l_imageSize, l_media.Fichier.ContentType), l_media.Fichier.ContentType); } return l_fileContentResult; }
public ActionResult Thumbnail(string path) { //TODO:Add security checks var files = new FilesRepository(); var image = files.ImageByPath(path); if (image != null) { var desiredSize = new ImageSize { Width = ThumbnailWidth, Height = ThumbnailHeight }; const string contentType = "image/png"; var thumbnailCreator = new ThumbnailCreator(new FitImageResizer()); using (var stream = new MemoryStream(image.Image1.ToArray())) { return File(thumbnailCreator.Create(stream, desiredSize, contentType), contentType); } } throw new HttpException(404, "File Not Found"); }
private FileContentResult CreateThumbnail(string physicalPath) { using (var fileStream = System.IO.File.OpenRead(physicalPath)) { var desiredSize = new ImageSize { Width = ThumbnailWidth, Height = ThumbnailHeight }; const string contentType = "image/png"; return File(thumbnailCreator.Create(fileStream, desiredSize, contentType), contentType); } }