public void RenderThumbnailToHttpOutput(ThumbnailOptions options) { HttpContext.Current.Response.ContentType = "image/jpeg"; HttpContext.Current.Response.WriteFile(GetThumbnailPhysicalPath(options)); }
public string GetThumbnailPhysicalPath(ThumbnailOptions options) { string thumbPath = WebUtility.MapPath("datastore/filemanager/thumbnails/" + options.GenerateFilename(sprocketFileID.Value)); if (File.Exists(thumbPath)) { if (new FileInfo(thumbPath).CreationTime > new FileInfo(PhysicalPath).CreationTime) { return(thumbPath); } else { File.Delete(thumbPath); } } Image img = Image.FromFile(PhysicalPath); Image thumb = new Bitmap(options.OuterWidth, options.OuterHeight); Graphics gfx = Graphics.FromImage(thumb); gfx.CompositingQuality = CompositingQuality.HighQuality; gfx.SmoothingMode = SmoothingMode.HighQuality; gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; Brush fill = new SolidBrush(options.BackgroundColor); Pen pen = new Pen(options.BorderColor, options.BorderWidth); pen.Alignment = PenAlignment.Inset; gfx.FillRectangle(fill, 0, 0, options.OuterWidth, options.OuterHeight); if (options.BorderWidth > 0) { gfx.DrawRectangle(pen, 0, 0, options.OuterWidth - 1, options.OuterHeight - 1); } Rectangle rect = new Rectangle(0, 0, img.Width, img.Height); if (img.Width > img.Height) { rect.Width = options.InnerWidth; rect.Height = (int)(((float)img.Height / (float)img.Width) * (float)options.InnerWidth); } else { rect.Height = options.InnerHeight; rect.Width = (int)(((float)img.Width / (float)img.Height) * (float)options.InnerHeight); } int xremain = options.OuterWidth - rect.Width; int yremain = options.OuterHeight - rect.Height; rect.X = xremain / 2; rect.Y = yremain / 2; if (xremain % 2 == 1) { rect.Width++; } if (yremain % 2 == 1) { rect.Height++; } gfx.DrawImage(img, rect); ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo encoder = null; for (int i = 0; i < encoders.Length; i++) { if (encoders[i].MimeType == "image/jpeg") { encoder = encoders[i]; break; } } if (encoder == null) { throw new SprocketException("Can't create a thumbnail because no JPEG encoder exists."); } EncoderParameters prms = new EncoderParameters(1); prms.Param[0] = new EncoderParameter(Encoder.Quality, 70L); thumb.Save(thumbPath, encoder, prms); img.Dispose(); thumb.Dispose(); return(thumbPath); }