public byte[] Create(Stream source, ImageSize desiredSize, string contentType) { Dictionary<string, ImageFormat> ImageFormats = new Dictionary<string, ImageFormat>(); ImageFormats.Add("image/png", ImageFormat.Png); ImageFormats.Add("image/gif", ImageFormat.Gif); ImageFormats.Add("image/jpeg", ImageFormat.Jpeg); ImageFormats.Add("image/jpg", ImageFormat.Jpeg); using (Image image = Image.FromStream(source)) { ImageSize originalSize = new ImageSize { Height = image.Height, Width = image.Width }; ImageSize size3 = Resize(originalSize, desiredSize); using (Bitmap bitmap = new Bitmap(size3.Width, size3.Height)) { this.ScaleImage(image, bitmap); using (MemoryStream stream = new MemoryStream()) { bitmap.Save(stream, ImageFormats[contentType]); return stream.ToArray(); } } } }
public ImageSize Resize(ImageSize originalSize, ImageSize targetSize) { float num = ((float)originalSize.Width) / ((float)originalSize.Height); int width = targetSize.Width; int height = targetSize.Height; if ((originalSize.Width > targetSize.Width) || (originalSize.Height > targetSize.Height)) { if (num > 1f) { height = (int)(((float)targetSize.Height) / num); } else { width = (int)(targetSize.Width * num); } } else { width = originalSize.Width; height = originalSize.Height; } return new ImageSize { Width = Math.Max(width, 1), Height = Math.Max(height, 1) }; }
private FileContentResult CreateThumbnail(string physicalPath) { using (FileStream stream = System.IO.File.OpenRead(physicalPath)) { ImageSize desiredSize = new ImageSize { Width = 80, Height = 80 }; var type = Path.GetExtension(physicalPath); type = type.Split('.')[1]; return base.File(Create(stream, desiredSize, "image/" + type.ToLower()), "image/" + type.ToLower()); } }
public FileContentResult getThumbnail(string path,int width,int height) { string physicalPath = Server.MapPath(path); using (FileStream stream = System.IO.File.OpenRead(physicalPath)) { ImageSize desiredSize = new ImageSize { Width = width, Height = height }; var type = Path.GetExtension(physicalPath); type = type.Split('.')[1]; return base.File(Create(stream, desiredSize, "image/" + type.ToLower()), "image/" + type.ToLower()); } }