/// <summary> /// Resizes and returns the resized content /// </summary> /// <param name="queryString">The query string.</param> /// <param name="fileContent">Content of the file.</param> /// <returns></returns> private Stream GetResized(NameValueCollection queryString, Stream fileContent) { try { ResizeSettings settings = new ResizeSettings(queryString); if (settings["mode"] == null || settings["mode"] == "clip") { settings.Add("mode", "max"); if (!string.IsNullOrEmpty(settings["width"]) && !string.IsNullOrEmpty(settings["height"])) { if (settings["width"].AsInteger() > settings["height"].AsInteger()) { settings.Remove("height"); } else { settings.Remove("width"); } } } MemoryStream resizedStream = new MemoryStream(); ImageBuilder.Current.Build(fileContent, resizedStream, settings, false); return(resizedStream); } catch { // if resize failed, just return original content return(fileContent); } }
public ResizeSettings Modify(ResizeSettings s) { if (!string.IsNullOrEmpty(s["preset"])) { string[] presets = s["preset"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string p in presets) { //Apply defaults if (defaults.ContainsKey(p)) { ResizeSettings dq = defaults[p]; foreach (string key in dq.Keys) { if (s[key] == null) { s[key] = dq[key]; //Overwrite null and missing values on defaults. } } } //Apply overrides if (settings.ContainsKey(p)) { ResizeSettings sq = settings[p]; foreach (string key in sq.Keys) { s[key] = sq[key]; //Overwrite null and missing values on defaults. } } } s.Remove("preset"); } return(s); }
void Pipeline_ImageMissing(System.Web.IHttpModule sender, System.Web.HttpContext context, Configuration.IUrlEventArgs e) { if (!string.IsNullOrEmpty(e.QueryString["404"])) { //Resolve the path to virtual or app-relative for string path = resolve404Path(e.QueryString["404"]); //Resolve to virtual path path = Util.PathUtils.ResolveAppRelative(path); //Merge commands from the 404 querystring with ones from the original image. ResizeSettings imageQuery = new ResizeSettings(e.QueryString); imageQuery.Normalize(); imageQuery.Remove("404"); //Remove the 404 ref ResizeSettings i404Query = new ResizeSettings(Util.PathUtils.ParseQueryString(path)); i404Query.Normalize(); //Overwrite new with old foreach (string key in i404Query.Keys) { if (key != null) { imageQuery[key] = i404Query[key]; } } path = PathUtils.AddQueryString(PathUtils.RemoveQueryString(path), PathUtils.BuildQueryString(imageQuery)); //Redirect context.Response.Redirect(path, true); } }
void Pipeline_ImageMissing(System.Web.IHttpModule sender, System.Web.HttpContext context, Configuration.IUrlEventArgs e) { if (!string.IsNullOrEmpty(e.QueryString["404"])) { //Resolve the path to virtual or app-relative for string path = resolve404Path(e.QueryString["404"]); //Resolve to virtual path path = Util.PathUtils.ResolveAppRelative(path); //Merge commands from the 404 querystring with ones from the original image. ResizeSettings imageQuery = new ResizeSettings(e.QueryString); imageQuery.Normalize(); imageQuery.Remove("404"); //Remove the 404 ref ResizeSettings i404Query = new ResizeSettings(Util.PathUtils.ParseQueryString(path)); i404Query.Normalize(); //Overwrite new with old foreach (string key in i404Query.Keys) if (key != null) imageQuery[key] = i404Query[key]; path = PathUtils.AddQueryString(PathUtils.RemoveQueryString(path), PathUtils.BuildQueryString(imageQuery)); //Redirect context.Response.Redirect(path, true); } }
void Pipeline_ImageMissing(System.Web.IHttpModule sender, System.Web.HttpContext context, Configuration.IUrlEventArgs e) { if (!string.IsNullOrEmpty(e.QueryString["404"])) { //Resolve the path to virtual or app-relative for string path = resolve404Path(e.QueryString["404"]); //Resolve to virtual path path = Util.PathUtils.ResolveAppRelative(path); // Merge commands from the 404 querystring with ones from the // original image. We start by sanitizing the querystring from // the image. ResizeSettings imageQuery = new ResizeSettings(e.QueryString); imageQuery.Normalize(); // Use the configured settings by default. var filterMode = this.filterMode; var except = this.except; // To support querystring-specifiable filter mode and exceptions, // uncomment the if block below. ////// If the imageQuery includes specific 404 command-filtering, ////// (*either* of the values), use those instead of the ////// configured defaults. ////if (!string.IsNullOrEmpty(e.QueryString["404.filterMode"]) || //// !string.IsNullOrEmpty(e.QueryString["404.except"])) ////{ //// filterMode = NameValueCollectionExtensions.Get(e.QueryString, "404.filterMode", FilterMode.ExcludeUnknownCommands); //// except = MatcherCollection.Parse(e.QueryString["404.except"]); ////} // remove all of the commands we're supposed to remove... we // clone the list of keys so that we're not modifying the collection // while we enumerate it. var shouldRemove = CreateRemovalMatcher(filterMode, except); var names = new List<string>(imageQuery.AllKeys); foreach (var name in names) { if (shouldRemove(name, imageQuery[name])) { imageQuery.Remove(name); } } // Always remove the '404', '404.filterMode', and '404.except' settings. imageQuery.Remove("404"); imageQuery.Remove("404.filterMode"); imageQuery.Remove("404.except"); ResizeSettings i404Query = new ResizeSettings(Util.PathUtils.ParseQueryString(path)); i404Query.Normalize(); //Overwrite new with old foreach (string key in i404Query.Keys) if (key != null) imageQuery[key] = i404Query[key]; path = PathUtils.AddQueryString(PathUtils.RemoveQueryString(path), PathUtils.BuildQueryString(imageQuery)); //Redirect context.Response.Redirect(path, true); } }
void Pipeline_ImageMissing(System.Web.IHttpModule sender, System.Web.HttpContext context, Configuration.IUrlEventArgs e) { if (!string.IsNullOrEmpty(e.QueryString["404"])) { //Resolve the path to virtual or app-relative for string path = resolve404Path(e.QueryString["404"]); //Resolve to virtual path path = Util.PathUtils.ResolveAppRelative(path); // Merge commands from the 404 querystring with ones from the // original image. We start by sanitizing the querystring from // the image. ResizeSettings imageQuery = new ResizeSettings(e.QueryString); imageQuery.Normalize(); // Use the configured settings by default. var filterMode = this.filterMode; var except = this.except; // To support querystring-specifiable filter mode and exceptions, // uncomment the if block below. ////// If the imageQuery includes specific 404 command-filtering, ////// (*either* of the values), use those instead of the ////// configured defaults. ////if (!string.IsNullOrEmpty(e.QueryString["404.filterMode"]) || //// !string.IsNullOrEmpty(e.QueryString["404.except"])) ////{ //// filterMode = NameValueCollectionExtensions.Get(e.QueryString, "404.filterMode", FilterMode.ExcludeUnknownCommands); //// except = MatcherCollection.Parse(e.QueryString["404.except"]); ////} // remove all of the commands we're supposed to remove... we // clone the list of keys so that we're not modifying the collection // while we enumerate it. var shouldRemove = CreateRemovalMatcher(filterMode, except); var names = new List <string>(imageQuery.AllKeys); foreach (var name in names) { if (shouldRemove(name, imageQuery[name])) { imageQuery.Remove(name); } } // Always remove the '404', '404.filterMode', and '404.except' settings. imageQuery.Remove("404"); imageQuery.Remove("404.filterMode"); imageQuery.Remove("404.except"); ResizeSettings i404Query = new ResizeSettings(Util.PathUtils.ParseQueryString(path)); i404Query.Normalize(); //Overwrite new with old foreach (string key in i404Query.Keys) { if (key != null) { imageQuery[key] = i404Query[key]; } } path = PathUtils.AddQueryString(PathUtils.RemoveQueryString(path), PathUtils.BuildQueryString(imageQuery)); //Redirect context.Response.Redirect(path, true); } }
private void ScaleAndSavePhotos(PhotoModel model, HttpRequestBase request) { var file = request.Files[0]; string extension = Path.GetExtension(file.FileName); string filename = file.FileName; //Copied Image and add watermark. ResizeSettings settings = new ResizeSettings(); settings.Add("format", "jpg"); settings.Add("quality", "90"); settings.Add("carve", "true"); settings.Add("scale", "down"); settings.Add("mode", "carve"); if (model.Watermark) { settings.Add("watermark", "wm"); } var path = Path.Combine(HttpContext.Current.Server.MapPath(string.Format("~/Albums/{0}", model.AlbumId))); if (Directory.Exists(path)) { //thumb is 75x75 //small is 240x159 //medium is 500 x 332 //large is 1024 x 680 Image img = Image.FromStream(file.InputStream); //PropertyItem[] properties = img.PropertyItems; //foreach (PropertyItem prop in properties) //{ //} bool vert = img.Height > img.Width; settings.Add("maxwidth", vert ? "680" : "1024"); //settings.Add("maxheight", vert ? "1024" : "680"); Bitmap copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false); copy.Save(Path.Combine(path, "Large\\" + filename)); settings.Remove("maxwidth"); //settings.Remove("maxheight"); settings.Remove("watermark"); settings.Add("maxwidth", vert ? "332" : "500"); //settings.Add("maxheight", vert ? "500" : "332"); copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false); copy.Save(Path.Combine(path, "Medium\\" + filename)); settings.Remove("maxwidth"); //settings.Remove("maxheight"); settings.Remove("watermark"); settings.Add("maxwidth", vert ? "200" : "301"); //settings.Add("maxheight", vert ? "301" : "200"); copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false); copy.Save(Path.Combine(path, "Small\\" + filename)); settings.Remove("maxwidth"); settings.Remove("maxwidth"); settings.Remove("watermark"); settings.Add("maxwidth", "75"); settings.Add("maxheight", "75"); copy = ImageResizer.ImageBuilder.Current.Build(img, settings, false); copy.Save(Path.Combine(path, "Thumb\\" + filename)); file.SaveAs(Path.Combine(path, "Original\\" + filename)); } }
public ResizeSettings Modify(ResizeSettings s) { if (!string.IsNullOrEmpty(s["preset"])) { string[] presets = s["preset"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); foreach (string p in presets) { //Apply defaults if (defaults.ContainsKey(p)) { ResizeSettings dq = defaults[p]; foreach (string key in dq.Keys) { if (s[key] == null) s[key] = dq[key]; //Overwrite null and missing values on defaults. } } //Apply overrides if (settings.ContainsKey(p)) { ResizeSettings sq = settings[p]; foreach (string key in sq.Keys) { s[key] = sq[key]; //Overwrite null and missing values on defaults. } } } s.Remove("preset"); } return s; }