/// <summary> /// Adds image watermark to an image. /// </summary> /// <param name="watermarkImage">Image to use as a watermark.</param> /// <param name="width">Width of watermark.</param> /// <param name="height">Height of watermark.</param> /// <param name="horizontalAlign">Horizontal alignment for watermark image. Can be "right", "left", or "center".</param> /// <param name="verticalAlign">Vertical alignment for watermark image. Can be "top", "bottom", or "middle".</param> /// <param name="opacity">Watermark text opacity. Should be between 0 and 100.</param> /// <param name="padding">Size of padding around watermark image in pixels.</param> /// <returns>Modified WebImage instance with added watermark.</returns> public WebImage AddImageWatermark( WebImage watermarkImage, int width = 0, int height = 0, string horizontalAlign = "Right", string verticalAlign = "Bottom", int opacity = 100, int padding = 5) { if (watermarkImage == null) { throw new ArgumentNullException("watermarkImage"); } if (width < 0) { throw new ArgumentOutOfRangeException( "width", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0)); } if (height < 0) { throw new ArgumentOutOfRangeException( "height", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0)); } if (((width == 0) && (height > 0)) || ((width > 0) && (height == 0))) { throw new ArgumentException(HelpersResources.WebImage_IncorrectWidthAndHeight); } if ((opacity < 0) || (opacity > 100)) { throw new ArgumentOutOfRangeException("opacity", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_Between, 0, 100)); } HorizontalAlign horizontalAlignEnum = ParseHorizontalAlign(horizontalAlign); VerticalAlign verticalAlignEnum = ParseVerticalAlign(verticalAlign); if (padding < 0) { throw new ArgumentOutOfRangeException( "padding", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0)); } WatermarkImageTransformation transformation = new WatermarkImageTransformation(watermarkImage.Clone(), width, height, horizontalAlignEnum, verticalAlignEnum, opacity, padding); _transformations.Add(transformation); return(this); }
public static MvcHtmlString GalleryImage(this HtmlHelper helper, string galleryImage) { if (string.IsNullOrEmpty(galleryImage)) return MvcHtmlString.Empty; var url = new UrlHelper(helper.ViewContext.RequestContext); var path = "~/public/UserFiles/gallery/" + HttpUtility.JavaScriptStringEncode(galleryImage); galleryImage = url.Content(path); if (galleryImage.EndsWith(".swf", StringComparison.OrdinalIgnoreCase)) return MvcHtmlString.Create(string.Format("<embed wmode='opaque' style='display: block' width='512' height='300' src='{0}'></embed>", galleryImage)); lock (lockObject) { var image = new WebImage(path); if (image.Width > 512 || image.Height > 300) { var newImage = image.Clone(); newImage.Resize(512, 300); if (newImage.Height < 300) { image.Resize(image.Width, 300); } else { image.Resize(512, image.Height); } image.Save(); } } return MvcHtmlString.Create(string.Format("<div class='gallery-image' style=\"background-image: url('{0}')\"></div>", galleryImage)); }
/// <summary> /// Adds image watermark to an image. /// </summary> /// <param name="watermarkImage">Image to use as a watermark.</param> /// <param name="width">Width of watermark.</param> /// <param name="height">Height of watermark.</param> /// <param name="horizontalAlign">Horizontal alignment for watermark image. Can be "right", "left", or "center".</param> /// <param name="verticalAlign">Vertical alignment for watermark image. Can be "top", "bottom", or "middle".</param> /// <param name="opacity">Watermark text opacity. Should be between 0 and 100.</param> /// <param name="padding">Size of padding around watermark image in pixels.</param> /// <returns>Modified WebImage instance with added watermark.</returns> public WebImage AddImageWatermark( WebImage watermarkImage, int width = 0, int height = 0, string horizontalAlign = "Right", string verticalAlign = "Bottom", int opacity = 100, int padding = 5) { if (watermarkImage == null) { throw new ArgumentNullException("watermarkImage"); } if (width < 0) { throw new ArgumentOutOfRangeException( "width", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0)); } if (height < 0) { throw new ArgumentOutOfRangeException( "height", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0)); } if (((width == 0) && (height > 0)) || ((width > 0) && (height == 0))) { throw new ArgumentException(HelpersResources.WebImage_IncorrectWidthAndHeight); } if ((opacity < 0) || (opacity > 100)) { throw new ArgumentOutOfRangeException("opacity", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_Between, 0, 100)); } HorizontalAlign horizontalAlignEnum = ParseHorizontalAlign(horizontalAlign); VerticalAlign verticalAlignEnum = ParseVerticalAlign(verticalAlign); if (padding < 0) { throw new ArgumentOutOfRangeException( "padding", String.Format(CultureInfo.InvariantCulture, CommonResources.Argument_Must_Be_GreaterThanOrEqualTo, 0)); } WatermarkImageTransformation transformation = new WatermarkImageTransformation(watermarkImage.Clone(), width, height, horizontalAlignEnum, verticalAlignEnum, opacity, padding); _transformations.Add(transformation); return this; }
/// <summary> /// Resizes the specified image. /// </summary> /// <param name="image">The image.</param> /// <param name="format">The format.</param> /// <param name="path">The path.</param> /// <param name="width">The width.</param> /// <param name="height">The height.</param> /// <param name="aspect">if set to <c>true</c> [aspect].</param> private static void Resize(WebImage image, string format, string path, int width, int height, bool aspect) { image.Clone().Resize(width, height, aspect).Save(path.AsPath(), format); }