From d45bc2bfce43c88d1731119e66927ccf847ecdad Mon Sep 17 00:00:00 2001 From: James South Date: Tue, 18 Mar 2014 23:47:31 +0000 Subject: [PATCH] Added configuration-less functionality. Former-commit-id: 49868772e9cc35d73fd0c84c4985f06d42b70b03 --- .../NET4/ImageProcessor.Web_NET4.csproj | 13 ++++ .../NET45/Config/ImageCacheSection.cs | 25 +++++--- .../NET45/Config/ImageProcessingSection.cs | 10 +++- .../NET45/Config/ImageSecuritySection.cs | 16 ++++- .../NET45/Config/Resources/cache.config | 1 + .../NET45/Config/Resources/processing.config | 39 ++++++++++++ .../NET45/Config/Resources/security.config | 4 ++ .../NET45/Helpers/ResourceHelpers.cs | 56 ++++++++++++++++++ .../NET45/ImageProcessor.Web_NET45.csproj | 8 +++ src/ImageProcessor/Processors/Resize.cs | 15 +++-- .../ImageProcessor.Web.Config.1.0.0.0.nupkg | Bin 0 -> 5331 bytes 11 files changed, 172 insertions(+), 15 deletions(-) create mode 100644 src/ImageProcessor.Web/NET45/Config/Resources/cache.config create mode 100644 src/ImageProcessor.Web/NET45/Config/Resources/processing.config create mode 100644 src/ImageProcessor.Web/NET45/Config/Resources/security.config create mode 100644 src/ImageProcessor.Web/NET45/Helpers/ResourceHelpers.cs create mode 100644 src/Nuget/ImageProcessor.Web.Config.1.0.0.0.nupkg diff --git a/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj b/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj index f8c863616..047451f26 100644 --- a/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj +++ b/src/ImageProcessor.Web/NET4/ImageProcessor.Web_NET4.csproj @@ -72,6 +72,7 @@ + @@ -90,6 +91,9 @@ ImageHelpers.cs + + ResourceHelpers.cs + StartUp.cs @@ -105,6 +109,15 @@ + + Config\Resources\cache.config + + + Config\Resources\processing.config + + + Config\Resources\security.config + Designer diff --git a/src/ImageProcessor.Web/NET45/Config/ImageCacheSection.cs b/src/ImageProcessor.Web/NET45/Config/ImageCacheSection.cs index b4b74f45d..6bda16a44 100644 --- a/src/ImageProcessor.Web/NET45/Config/ImageCacheSection.cs +++ b/src/ImageProcessor.Web/NET45/Config/ImageCacheSection.cs @@ -1,16 +1,22 @@ -// ----------------------------------------------------------------------- +// -------------------------------------------------------------------------------------------------------------------- // -// Copyright (c) James South. -// Licensed under the Apache License, Version 2.0. +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. // -// ----------------------------------------------------------------------- +// +// Represents an image cache section within a configuration file. +// +// -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Web.Config { #region Using using System.Configuration; + using System.IO; + using System.Xml; using ImageProcessor.Extensions; + using ImageProcessor.Web.Helpers; #endregion @@ -24,7 +30,7 @@ namespace ImageProcessor.Web.Config /// /// The name of the cache folder. [ConfigurationProperty("virtualPath", DefaultValue = "~/app_data/cache", IsRequired = true)] - [StringValidator(MinLength = 3, MaxLength = 200)] + [StringValidator(MinLength = 3, MaxLength = 256)] public string VirtualPath { get @@ -45,7 +51,7 @@ namespace ImageProcessor.Web.Config /// /// The maximum number of days to store an image in the cache. /// Defaults to 28 if not set. - [ConfigurationProperty("maxDays", DefaultValue = "28", IsRequired = false)] + [ConfigurationProperty("maxDays", DefaultValue = "365", IsRequired = false)] [IntegerValidator(ExcludeRange = false, MinValue = 0)] public int MaxDays { @@ -73,7 +79,12 @@ namespace ImageProcessor.Web.Config return imageCacheSection; } - return new ImageCacheSection(); + string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Config.Resources.cache.config"); + XmlReader reader = new XmlTextReader(new StringReader(section)); + imageCacheSection = new ImageCacheSection(); + imageCacheSection.DeserializeSection(reader); + + return imageCacheSection; } } } diff --git a/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs b/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs index 1b0ed5952..7111521af 100644 --- a/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs +++ b/src/ImageProcessor.Web/NET45/Config/ImageProcessingSection.cs @@ -13,7 +13,10 @@ namespace ImageProcessor.Web.Config { #region Using using System.Configuration; + using System.IO; using System.Linq; + using System.Xml; + using ImageProcessor.Web.Helpers; #endregion /// @@ -69,7 +72,12 @@ namespace ImageProcessor.Web.Config return imageProcessingSection; } - return new ImageProcessingSection(); + string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Config.Resources.processing.config"); + XmlReader reader = new XmlTextReader(new StringReader(section)); + imageProcessingSection = new ImageProcessingSection(); + imageProcessingSection.DeserializeSection(reader); + + return imageProcessingSection; } #endregion diff --git a/src/ImageProcessor.Web/NET45/Config/ImageSecuritySection.cs b/src/ImageProcessor.Web/NET45/Config/ImageSecuritySection.cs index dac3dd84e..824cf8277 100644 --- a/src/ImageProcessor.Web/NET45/Config/ImageSecuritySection.cs +++ b/src/ImageProcessor.Web/NET45/Config/ImageSecuritySection.cs @@ -13,6 +13,11 @@ namespace ImageProcessor.Web.Config #region Using using System; using System.Configuration; + using System.IO; + using System.Xml; + + using ImageProcessor.Web.Helpers; + #endregion /// @@ -55,8 +60,8 @@ namespace ImageProcessor.Web.Config /// Gets or sets the maximum allowed remote file size in bytes for the application. /// /// The maximum number of days to store an image in the cache. - /// Defaults to 524288 (512kb) if not set. - [ConfigurationProperty("maxBytes", DefaultValue = "524288", IsRequired = true)] + /// Defaults to 4194304 (4Mb) if not set. + [ConfigurationProperty("maxBytes", DefaultValue = "4194304", IsRequired = true)] public int MaxBytes { get @@ -111,7 +116,12 @@ namespace ImageProcessor.Web.Config return imageSecuritySection; } - return new ImageSecuritySection(); + string section = ResourceHelpers.ResourceAsString("ImageProcessor.Web.Config.Resources.security.config"); + XmlReader reader = new XmlTextReader(new StringReader(section)); + imageSecuritySection = new ImageSecuritySection(); + imageSecuritySection.DeserializeSection(reader); + + return imageSecuritySection; } #endregion diff --git a/src/ImageProcessor.Web/NET45/Config/Resources/cache.config b/src/ImageProcessor.Web/NET45/Config/Resources/cache.config new file mode 100644 index 000000000..1caab6358 --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Config/Resources/cache.config @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/ImageProcessor.Web/NET45/Config/Resources/processing.config b/src/ImageProcessor.Web/NET45/Config/Resources/processing.config new file mode 100644 index 000000000..0cb384b96 --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Config/Resources/processing.config @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ImageProcessor.Web/NET45/Config/Resources/security.config b/src/ImageProcessor.Web/NET45/Config/Resources/security.config new file mode 100644 index 000000000..99cd3d3ff --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Config/Resources/security.config @@ -0,0 +1,4 @@ + + + + diff --git a/src/ImageProcessor.Web/NET45/Helpers/ResourceHelpers.cs b/src/ImageProcessor.Web/NET45/Helpers/ResourceHelpers.cs new file mode 100644 index 000000000..fe8dc523b --- /dev/null +++ b/src/ImageProcessor.Web/NET45/Helpers/ResourceHelpers.cs @@ -0,0 +1,56 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Provides helper methods for working with resources. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Web.Helpers +{ + using System.IO; + using System.Reflection; + using System.Text; + + /// + /// Provides helper methods for working with resources. + /// + public class ResourceHelpers + { + /// + /// Converts an assembly resource into a string. + /// + /// + /// The resource. + /// + /// + /// The assembly. + /// + /// + /// The character encoding to return the resource in. + /// + /// + /// The . + /// + public static string ResourceAsString(string resource, Assembly assembly = null, Encoding encoding = null) + { + assembly = assembly ?? Assembly.GetExecutingAssembly(); + encoding = encoding ?? Encoding.UTF8; + + using (MemoryStream ms = new MemoryStream()) + { + using (Stream manifestResourceStream = assembly.GetManifestResourceStream(resource)) + { + if (manifestResourceStream != null) + { + manifestResourceStream.CopyTo(ms); + } + } + + return encoding.GetString(ms.GetBuffer()).Replace('\0', ' ').Trim(); + } + } + } +} diff --git a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj index e40a4f641..10b2f0923 100644 --- a/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj +++ b/src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj @@ -57,6 +57,7 @@ + @@ -72,6 +73,13 @@ + + Designer + + + Designer + + diff --git a/src/ImageProcessor/Processors/Resize.cs b/src/ImageProcessor/Processors/Resize.cs index d6d34390f..0c66e8de9 100644 --- a/src/ImageProcessor/Processors/Resize.cs +++ b/src/ImageProcessor/Processors/Resize.cs @@ -446,12 +446,19 @@ namespace ImageProcessor.Processors graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.PixelOffsetMode = PixelOffsetMode.HighQuality; - graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; - graphics.Clear(backgroundColor); - Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight); - graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel); + // An unwanted border appears when using InterpolationMode.HighQualityBicubic to resize the image + // as the algorithm appears to be pulling averaging detail from surrounding pixels beyond the edge + // of the image. Using the ImageAttributes class to specify that the pixels beyond are simply mirror + // images of the pixels within solves this problem. + using (ImageAttributes wrapMode = new ImageAttributes()) + { + wrapMode.SetWrapMode(WrapMode.TileFlipXY); + graphics.Clear(backgroundColor); + Rectangle destRect = new Rectangle(destinationX, destinationY, destinationWidth, destinationHeight); + graphics.DrawImage(image, destRect, 0, 0, sourceWidth, sourceHeight, GraphicsUnit.Pixel, wrapMode); + } // Reassign the image. image.Dispose(); diff --git a/src/Nuget/ImageProcessor.Web.Config.1.0.0.0.nupkg b/src/Nuget/ImageProcessor.Web.Config.1.0.0.0.nupkg new file mode 100644 index 0000000000000000000000000000000000000000..3acf89cfbfe8ed4edb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