public static Stream Resize(int width, int height, Stream image, string fileName) { Stream fileStream = new MemoryStream(); Image tempImage = Bitmap.FromStream(image); long maxFactor = width * height; long imageFactor = tempImage.Width * tempImage.Height; if (maxFactor < imageFactor) { using (ImageResizer resizer = new ImageResizer()) { resizer.MimeType = Path.GetExtension(fileName); resizer.SourceImage = tempImage; resizer.Background = ColorTranslator.FromHtml("#fff"); resizer.Mode = ImageResizer.ResizeMode.KeepOriginalAspect; resizer.Width = width; resizer.Height = height; resizer.Process(); resizer.ResultImage.Save(fileStream, tempImage.RawFormat); } } else { image.Seek(0, SeekOrigin.Begin); image.CopyTo(fileStream); } return fileStream; }
private void StartAsyncTask(Object workItemState) { try { string location = _context.Request.QueryString["image"]; if (location == null) //throw new HttpException("Invalid request."); return; string extension; string contentType; try { extension = Path.GetExtension(location).ToLower(); } catch { throw new HttpException("Invalid request: " + location); } switch (extension) { case ".jpg": contentType = "image/jpeg"; break; case ".jpeg": contentType = "image/jpeg"; break; case ".png": contentType = "image/png"; break; case ".gif": contentType = "image/gif"; break; case ".bmp": contentType = "image/bmp"; break; default: throw new HttpException(404, "Invalid request: " + location); } string absoluteLocation = _context.Server.MapPath("~/" + location); var ecommerceService = (IECommerce) DependencyResolver.Current.GetService(typeof (IECommerce)); //var config = ImageProviderWebSectionGroup.FromCurrentConfiguration().ImageProvider; //if (config.Presets.Count == 0) // throw new ConfigurationErrorsException("No resize presets defined."); //ImageProviderPresetElement preset; var preset = new tbl_ProdImageVerNames(); if (_context.Request["preset"] != null) { preset = ecommerceService.GetProductImageVersionByName(_context.Request.QueryString["preset"]); if (preset == null) throw new ArgumentException("The specified preset name is not valid.", "preset"); } _context.Response.Cache.SetCacheability(HttpCacheability.Public); _context.Response.Cache.SetExpires(DateTime.Now.AddDays(3)); if (preset.VN_Width == 0 && preset.VN_Width == 0 && File.Exists(absoluteLocation)) { _context.Response.ContentType = contentType; using (FileStream f = File.OpenRead(absoluteLocation)) { f.Write(_context.Response.OutputStream, 65536); } return; } string absoluteCacheDirectoryLocation = _context.Server.MapPath(String.IsNullOrWhiteSpace(preset.VN_Path) ? "/Images/Versions" : preset.VN_Path.TrimEnd('/')); string absoluteCacheFileLocation = new StringBuilder() .Append(absoluteCacheDirectoryLocation).Append(Path.DirectorySeparatorChar) .Append(preset.VN_Width).Append("x").Append(preset.VN_Height) .Append("_") .Append( (ImageResizer.ResizeMode) Enum.Parse(typeof (ImageResizer.ResizeMode), preset.VN_Mode.GetValueOrDefault( (int) ImageResizer.ResizeMode.CropToDestinationAspect).ToString(CultureInfo.InvariantCulture))) .Append("_").Append(Path.GetFileName(location)).ToString(); if (File.Exists(absoluteCacheFileLocation)) { _context.Response.ContentType = contentType; using (FileStream f = File.OpenRead(absoluteCacheFileLocation)) { f.Write(_context.Response.OutputStream, 65536); } return; } if (File.Exists(absoluteLocation)) { using (var resizer = new ImageResizer(absoluteLocation)) { resizer.Background = String.IsNullOrEmpty(preset.VN_Background) ? ColorTranslator.FromHtml("#FFF") : ColorTranslator.FromHtml(preset.VN_Background); resizer.Mode = preset.VN_Mode.HasValue ? (ImageResizer.ResizeMode) Enum.Parse(typeof (ImageResizer.ResizeMode), preset.VN_Mode.Value.ToString(CultureInfo.InvariantCulture)) : ImageResizer.ResizeMode.CropToDestinationAspect; resizer.Width = preset.VN_Width; resizer.Height = preset.VN_Height; resizer.Process(); _context.Response.ContentType = contentType; if (!Directory.Exists(absoluteCacheDirectoryLocation)) Directory.CreateDirectory(absoluteCacheDirectoryLocation); using (var output = new MemoryStream()) { resizer.SaveAs(output); output.WriteTo(_context.Response.OutputStream); } resizer.SaveAs(absoluteCacheFileLocation); } } } catch (Exception e) { Log.Error(e.Message); } finally { _completed = true; _callback(this); } }