public string GetUrl(int width, int height, bool fill, DynamicImageFormat format) { Composition image = new Composition(); image.ImageFormat = format; ImageLayer imageLayer = new ImageLayer(); ZeusImageSource source = new ZeusImageSource(); source.ContentID = ID; imageLayer.Source = source; ResizeFilter resizeFilter = new ResizeFilter(); resizeFilter.Mode = fill ? ResizeMode.UniformFill : ResizeMode.Uniform; resizeFilter.Width = Unit.Pixel(width); resizeFilter.Height = Unit.Pixel(height); imageLayer.Filters.Add(resizeFilter); image.Layers.Add(imageLayer); return ImageUrlGenerator.GetImageUrl(image); /*old code replaced * return new DynamicImageBuilder() .WithLayer( LayerBuilder.Image.SourceImage(this).WithFilter(FilterBuilder.Resize.To(width, height, fill))) .Url; */ }
public static string Image(this HtmlHelper helper, Image image, int width, int height, bool fill, string defaultImage, DynamicImageFormat format) { string url = ImageUrl(helper, image, width, height, fill, defaultImage, format); if (string.IsNullOrEmpty(url)) return string.Empty; return ImageTag(helper, image, url); }
public static BitmapEncoder GetEncoder(DynamicImageFormat format) { switch (format) { case DynamicImageFormat.Bmp : return new BmpBitmapEncoder(); case DynamicImageFormat.Gif : return new GifBitmapEncoder(); case DynamicImageFormat.Jpeg : return new JpegBitmapEncoder(); case DynamicImageFormat.Png : return new PngBitmapEncoder(); default : throw new NotSupportedException(); } }
public static BitmapEncoder GetEncoder(DynamicImageFormat format) { switch (format) { case DynamicImageFormat.Bmp: return(new BmpBitmapEncoder()); case DynamicImageFormat.Gif: return(new GifBitmapEncoder()); case DynamicImageFormat.Jpeg: return(new JpegBitmapEncoder()); case DynamicImageFormat.Png: return(new PngBitmapEncoder()); default: throw new NotSupportedException(); } }
public CompositionBuilder ImageFormat(DynamicImageFormat imageFormat) { _composition.ImageFormat = imageFormat; return(this); }
// Methods internal static string ToQueryString( int width, int height, int clientCacheDuration, int serverCacheDuration, RotateFlipType rotateFlip, bool drawGrayscale, bool drawSepia, DynamicText text, DynamicImageFormat imageFormat, KeyValuePair<Type, IDictionary<string, string>>? imageCreator, Dictionary<Type, IDictionary<string, string>> imageTransformations, TextContainerSizeType sizeType, CaptchaStyle distortionStyle, ReadnessLevel readnessLevel, Color backColor) { string dynamicImageQueryString = DynamicImageProvider.ToQueryString( null, width, height, clientCacheDuration, serverCacheDuration, rotateFlip, drawGrayscale, drawSepia, text, imageFormat, imageCreator, imageTransformations); List<string> parameters = new List<string>(); string parameterFormat = "{0}={1}"; // size type if (TextContainerSizeType.Specified != sizeType) { parameters.Add(string.Format(parameterFormat, TextDependentImageCreator.TEXTCONTSIZETYPE_KEY, sizeType)); } // distortion style if (CaptchaStyle.Confetti != distortionStyle) { parameters.Add(string.Format(parameterFormat, CaptchaImageTransformation.DISTSTYLE_KEY, distortionStyle)); } // readness level if (ReadnessLevel.Normal != readnessLevel) { parameters.Add(string.Format(parameterFormat, CaptchaImageTransformation.READLEVEL_KEY, readnessLevel)); } // back color if (Color.White != backColor) { parameters.Add(string.Format(parameterFormat, CaptchaImageTransformation.CAPTCHABACKCOLOR_KEY, backColor.ToArgb())); } return (0 < parameters.Count) ? string.Concat(dynamicImageQueryString, "&", string.Join("&", parameters.ToArray())) : dynamicImageQueryString; }
internal static string ToQueryString( string source, int? width, int? height, int clientCacheDuration, int serverCacheDuration, RotateFlipType rotateFlip, bool drawGrayscale, bool drawSepia, DynamicText text, DynamicImageFormat imageFormat, KeyValuePair<Type, IDictionary<string, string>>? imageCreator, Dictionary<Type, IDictionary<string, string>> imageTransformations) { HttpServerUtility server = HttpContext.Current.Server; string parameterFormat = "{0}={1}"; Dictionary<string, string> parameters = new Dictionary<string, string>(); // width if (width.HasValue && (0 < width.Value)) { parameters.Add( ResizeImageTransformation.SIZEW_KEY, string.Format(parameterFormat, ResizeImageTransformation.SIZEW_KEY, width.Value)); } // height if (height.HasValue && (0 < height.Value)) { parameters.Add( ResizeImageTransformation.SIZEH_KEY, string.Format(parameterFormat, ResizeImageTransformation.SIZEH_KEY, height.Value)); } // client cache duration if (0 < clientCacheDuration) { parameters.Add( DynamicImageProvider.CLIENTCACHEDUR_KEY, string.Format(parameterFormat, DynamicImageProvider.CLIENTCACHEDUR_KEY, clientCacheDuration)); } // server cache duration if (0 < serverCacheDuration) { parameters.Add( DynamicImageProvider.SERVERCACHEDUR_KEY, string.Format(parameterFormat, DynamicImageProvider.SERVERCACHEDUR_KEY, serverCacheDuration)); } // rotate flip if (RotateFlipType.RotateNoneFlipNone != rotateFlip) { parameters.Add( RotateFlipImageTransformation.ROTATETYPE_KEY, string.Format(parameterFormat, RotateFlipImageTransformation.ROTATETYPE_KEY, rotateFlip)); } // grayscale if (drawGrayscale) { parameters.Add( GrayscaleImageTransformation.GRAYSCALE_KEY, string.Format(parameterFormat, GrayscaleImageTransformation.GRAYSCALE_KEY, drawGrayscale)); } // sepia if (drawSepia) { parameters.Add( SepiaImageTransformation.SEPIA_KEY, string.Format(parameterFormat, SepiaImageTransformation.SEPIA_KEY, drawSepia)); } if ((null != text) && !string.IsNullOrEmpty(text.Value)) { // text parameters.Add( TextImageTransformation.TEXTVALUE_KEY, string.Format(parameterFormat, TextImageTransformation.TEXTVALUE_KEY, server.UrlEncode(text.Value))); // text font if (null != text.Font) { string stringFont = new FontConverter().ConvertToString(text.Font); if ("Arial;10.0f" != stringFont) { parameters.Add( TextImageTransformation.TEXTFONT_KEY, string.Format(parameterFormat, TextImageTransformation.TEXTFONT_KEY, server.UrlEncode(stringFont))); } } // text color if (Color.Black != text.Color) { parameters.Add( TextImageTransformation.TEXTCOLOR_KEY, string.Format(parameterFormat, TextImageTransformation.TEXTCOLOR_KEY, text.Color.ToArgb())); } // text halignment if (StringAlignment.Far != text.HorizontalAlign) { parameters.Add( TextImageTransformation.TEXTHEIGHTALIGN_KEY, string.Format(parameterFormat, TextImageTransformation.TEXTHEIGHTALIGN_KEY, text.HorizontalAlign)); } // text valignment if (StringAlignment.Far != text.VerticalAlign) { parameters.Add( TextImageTransformation.TEXTWIDTHALIGN_KEY, string.Format(parameterFormat, TextImageTransformation.TEXTWIDTHALIGN_KEY, text.VerticalAlign)); } } // image format if (DynamicImageFormat.Original != imageFormat) { parameters.Add( DynamicImageProvider.IMAGEFORMAT_KEY, string.Format(parameterFormat, DynamicImageProvider.IMAGEFORMAT_KEY, imageFormat)); } if ((null == imageCreator) || (null == imageCreator.Value.Key)) { // source if (!string.IsNullOrEmpty(source)) { parameters.Add( LocalizedImageCreation.SOURCE_KEY, string.Format(parameterFormat, LocalizedImageCreation.SOURCE_KEY, server.UrlEncode(source))); } } // set for last the custom creators and transformations // to be able to yield duplicate keys // image creation type if ((null != imageCreator) && (null != imageCreator.Value.Key)) { parameters.Add( DynamicImageProvider.CREATIONTYPE_KEY, string.Format(parameterFormat, DynamicImageProvider.CREATIONTYPE_KEY, server.UrlEncode(imageCreator.Value.Key.AssemblyQualifiedName))); if ((null != imageCreator.Value.Value) && (0 < imageCreator.Value.Value.Count)) { foreach (KeyValuePair<string, string> param in imageCreator.Value.Value) { if (!string.IsNullOrEmpty(param.Key)) { if (parameters.ContainsKey(param.Key) || param.Key.StartsWith(DynamicImageProvider.TRANSFTYPEPREFIX_KEY, StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException("Duplicate parameter key in query string (creation type)."); } parameters.Add(param.Key, string.Format(parameterFormat, server.UrlEncode(param.Key), server.UrlEncode(param.Value))); } } } } // image tranformations types if ((null != imageTransformations) && (0 < imageTransformations.Count)) { int i = 0; foreach (Type t in imageTransformations.Keys) { string typeKey = string.Format("{0}{1}", DynamicImageProvider.TRANSFTYPEPREFIX_KEY, i++); parameters.Add( typeKey, string.Format(parameterFormat, typeKey, server.UrlEncode(t.AssemblyQualifiedName))); if ((null != imageTransformations[t]) && (0 < imageTransformations[t].Count)) { foreach (KeyValuePair<string, string> param in imageTransformations[t]) { if (parameters.ContainsKey(param.Key) || param.Key.StartsWith(DynamicImageProvider.TRANSFTYPEPREFIX_KEY, StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException("Duplicate parameter key in query string (creation type)."); } parameters.Add(param.Key, string.Format(parameterFormat, server.UrlEncode(param.Key), server.UrlEncode(param.Value))); } } } } return (parameters.Count > 0) ? string.Join("&", new List<string>(parameters.Values).ToArray()) : string.Empty; }
/// <summary> /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the <see cref="T:System.Web.IHttpHandler"/> interface. /// </summary> /// <param name="context">An <see cref="T:System.Web.HttpContext"/> object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param> public void ProcessRequest(HttpContext context) { this.context = context; // native params this.Parameters = this.Context.Request.QueryString; if (!string.IsNullOrEmpty(this.Parameters[DynamicImageProvider.CLIENTCACHEDUR_KEY])) { this.clientCacheDuration = int.Parse(this.Parameters[DynamicImageProvider.CLIENTCACHEDUR_KEY]); } if (!string.IsNullOrEmpty(this.Parameters[DynamicImageProvider.SERVERCACHEDUR_KEY])) { this.serverCacheDuration = int.Parse(this.Parameters[DynamicImageProvider.SERVERCACHEDUR_KEY]); } if (!string.IsNullOrEmpty(this.Parameters[DynamicImageProvider.IMAGEFORMAT_KEY])) { this.imageFormat = (DynamicImageFormat)Enum.Parse(typeof(DynamicImageFormat), this.Parameters[DynamicImageProvider.IMAGEFORMAT_KEY]); } // begin the image processing Image image = null; ImageFormat imageFormat = null; // try to load it from server cache if (0 < this.serverCacheDuration) { // try to get it from the server cache; // the source can be pointless sometimes (in child classes), so the same source value for different images // the only thing that uniquely identifies an image si the query string of the image request; // this approach comes with a risk: the parameters in the query string should be in an exact order // so everything will be ok as long as the query string will be implemented in DynamicImage.ToQueryString(); // a querystring approach strategy is required when the image request will have support from the client side DynamicImageCacheItem cacheItem = this.Context.Cache[this.ServerCacheKey] as DynamicImageCacheItem; if (null != cacheItem) { // an identic one cached, no processing needed image = cacheItem.Image; imageFormat = cacheItem.ImageFormat; } } // the image is not cached so create and transform it if (null == image) { // an image creator is specified if (!string.IsNullOrEmpty(this.Parameters[DynamicImageProvider.CREATIONTYPE_KEY])) { Type t = Type.GetType(this.Parameters[DynamicImageProvider.CREATIONTYPE_KEY]); IImageCreator imageCreator = Activator.CreateInstance(t) as IImageCreator; image = imageCreator.Get(this.Parameters); } else { // create the image and get the original format image = this.CreateImage(this.Parameters); if (null == image) { this.Context.Response.StatusCode = (int)HttpStatusCode.NotFound; this.Context.Response.End(); return; } } // handle the original or requested image format if (DynamicImageFormat.Original == this.imageFormat) { imageFormat = image.RawFormat; } else { switch (this.imageFormat) { case DynamicImageFormat.Bmp: { imageFormat = ImageFormat.Bmp; break; } case DynamicImageFormat.Gif: { imageFormat = ImageFormat.Gif; break; } case DynamicImageFormat.Jpeg: { imageFormat = ImageFormat.Jpeg; break; } case DynamicImageFormat.Png: { imageFormat = ImageFormat.Png; break; } default: { throw new NotSupportedException("imageFormat"); } } } // transform the image (with native transformations) this.TransformImage(ref image, this.Parameters); // transform the image (with custom transformations) foreach (string keyParam in this.Parameters.AllKeys) { if ((null != keyParam) && (keyParam.StartsWith(DynamicImageProvider.TRANSFTYPEPREFIX_KEY, StringComparison.InvariantCultureIgnoreCase))) { Type t = Type.GetType(this.Parameters[keyParam]); IImageTransformation imageTransformation = Activator.CreateInstance(t) as IImageTransformation; imageTransformation.Apply(ref image, this.Parameters); } } // makes sense to add it to server cache if not already if (0 < serverCacheDuration) { DynamicImageCacheItem cacheItem = new DynamicImageCacheItem(image, imageFormat); this.Context.Cache.Add( this.ServerCacheKey, cacheItem, null, DateTime.Now.AddMinutes(serverCacheDuration), TimeSpan.Zero, CacheItemPriority.Normal, null); } } // resolve the client cache if (0 < clientCacheDuration) { this.Context.Response.CacheControl = "private"; this.Context.Response.Expires = clientCacheDuration; } // finalize the request this.Context.Response.ContentType = this.ImageFormatToMimeType(imageFormat); // write image, avoid seekable stream issues by using an intermediate stream using (MemoryStream ms = new MemoryStream()) { image.Save(ms, imageFormat); ms.WriteTo(this.Context.Response.OutputStream); } this.Context.Response.End(); }
public DynamicImageBuilder ImageFormat(DynamicImageFormat imageFormat) { _dynamicImage.ImageFormat = imageFormat; return this; }
public static string ImageUrl(this HtmlHelper helper, Image image, int width, int height, bool fill, string defaultImage, DynamicImageFormat format) { string result = defaultImage; // only generate url if image exists if (image != null) { // special code for image without resizing if (width == 0 && height == 0) { result = new CompositionBuilder() .WithLayer(LayerBuilder.Image.SourceImage(image)) .Url; } // generate resized image url else { if (image is CroppedImage) { CroppedImage cImage = (CroppedImage)image; result = cImage.GetUrl(width, height, fill, format); } else { result = image.GetUrl(width, height, fill, format); } } } return result; }
public static string ImageUrl(this HtmlHelper helper, Image image, int width, int height, bool fill, DynamicImageFormat format) { return ImageUrl(helper, image, width, height, fill, string.Empty, format); }
public string GetUrl(int width, int height, bool fill, DynamicImageFormat format, bool isResize) { //first construct the crop var imageSource = new ZeusImageSource(); imageSource.ContentID = this.ID; if (this.Data == null) return ""; // generate resized image url // set image format var dynamicImage = new SoundInTheory.DynamicImage.Composition(); dynamicImage.ImageFormat = format; // create image layer wit ha source var imageLayer = new ImageLayer(); imageLayer.Source = imageSource; // add filters if (!(TopLeftXVal == 0 && TopLeftYVal == 0 && CropWidth == 0 && CropHeight == 0)) { var cropFilter = new CropFilter { Enabled = true, X = this.TopLeftXVal, Y = this.TopLeftYVal, Width = this.CropWidth, Height = this.CropHeight }; if (!isResize) imageLayer.Filters.Add(cropFilter); } if (width > 0 && height > 0) { var resizeFilter = new ResizeFilter { Mode = isResize ? ResizeMode.Uniform : ResizeMode.UniformFill, Width = SoundInTheory.DynamicImage.Unit.Pixel(width), Height = SoundInTheory.DynamicImage.Unit.Pixel(height) }; imageLayer.Filters.Add(resizeFilter); } else if (width > 0) { var resizeFilter = new ResizeFilter { Mode = ResizeMode.UseWidth, Width = SoundInTheory.DynamicImage.Unit.Pixel(width) }; imageLayer.Filters.Add(resizeFilter); } else if (height > 0) { var resizeFilter = new ResizeFilter { Mode = ResizeMode.UseHeight, Height = SoundInTheory.DynamicImage.Unit.Pixel(height) }; imageLayer.Filters.Add(resizeFilter); } // add the layer dynamicImage.Layers.Add(imageLayer); // generate url return ImageUrlGenerator.GetImageUrl(dynamicImage); }
public string GetUrl(int width, int height, bool fill, DynamicImageFormat format) { return GetUrl(width, height, fill, format, false); }
// Methods /// <summary> /// Utility method that returns a query string representation based on the provided parameter values. /// </summary> /// <param name="source">The image source.</param> /// <param name="width">The image width.</param> /// <param name="height">The image height.</param> /// <param name="clientCacheDuration">The image client cache duration.</param> /// <param name="serverCacheDuration">The image server cache duration.</param> /// <param name="rotateFlip">The image rotate flip type.</param> /// <param name="drawGrayscale">The image draw grayscale type.</param> /// <param name="drawSepia">The image draw sepia type.</param> /// <param name="text">The image dynamic text.</param> /// <param name="gradientBackground">The button gradient background.</param> /// <param name="sizeType">The button size type.</param> /// <param name="textRenderTime">The time when the text is rendered.</param> internal static string ToQueryString( int width, int height, int clientCacheDuration, int serverCacheDuration, RotateFlipType rotateFlip, bool drawGrayscale, bool drawSepia, DynamicText text, DynamicImageFormat imageFormat, KeyValuePair<Type, IDictionary<string, string>>? imageCreator, Dictionary<Type, IDictionary<string, string>> imageTransformations, GradientBackground gradientBackground, TextContainerSizeType sizeType) { string dynamicImageQueryString = DynamicImageProvider.ToQueryString( null, width, height, clientCacheDuration, serverCacheDuration, rotateFlip, drawGrayscale, drawSepia, text, imageFormat, imageCreator, imageTransformations); List<string> parameters = new List<string>(); string parameterFormat = "{0}={1}"; if (null != gradientBackground) { // gradient border color if (Color.Gray != gradientBackground.BorderColor) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGCOLOR_KEY, gradientBackground.BorderColor.ToArgb())); } // gradient start color if (Color.Brown != gradientBackground.GradientStartColor) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGSTARTCOLOR_KEY, gradientBackground.GradientStartColor.ToArgb())); } // gradient end color if (Color.White != gradientBackground.GradientEndColor) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGENDCOLOR_KEY, gradientBackground.GradientEndColor.ToArgb())); } // gradient round corner radius if (0 <= gradientBackground.RoundCornerRadius) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGRCORNER_KEY, gradientBackground.RoundCornerRadius)); } // gradient border width if (0 <= gradientBackground.BorderWidth) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGBWIDTH_KEY, gradientBackground.BorderWidth)); } // gradient type if (GradientType.BackwardDiagonal != gradientBackground.Type) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGTYPE_KEY, gradientBackground.Type)); } // gradient inner border color if (Color.Gray != gradientBackground.InnerBorderColor) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGIBCOLOR_KEY, gradientBackground.InnerBorderColor.ToArgb())); } // gradient inner border width if (0 <= gradientBackground.InnerBorderWidth) { parameters.Add(string.Format(parameterFormat, GradientButtonImageTransformation.GRADIENTBKGIBWIDTH_KEY, gradientBackground.InnerBorderWidth)); } } // size type if (TextContainerSizeType.Specified != sizeType) { parameters.Add(string.Format(parameterFormat, TextDependentImageCreator.TEXTCONTSIZETYPE_KEY, sizeType)); } return (0 < parameters.Count) ? string.Concat(dynamicImageQueryString, "&", string.Join("&", parameters.ToArray())) : dynamicImageQueryString; }