Browse Source

Removing background color from RoundedCorners

Former-commit-id: cd267a63b69e5c720ed5508468c0c3e9a777583a
pull/17/head
James South 12 years ago
parent
commit
2ed23db3ce
  1. 2
      src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs
  2. 2
      src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs
  3. 12
      src/ImageProcessor.Web/Processors/RoundedCorners.cs
  4. 61
      src/ImageProcessor/Imaging/RoundedCornerLayer.cs
  5. 13
      src/ImageProcessor/Processors/RoundedCorners.cs

2
src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs

@ -396,7 +396,7 @@ namespace ImageProcessor.UnitTests
{
imageFactory.Load(file.FullName);
Image original = (Image)imageFactory.Image.Clone();
imageFactory.RoundedCorners(new Imaging.RoundedCornerLayer(5, true, true, true, true));
imageFactory.RoundedCorners(new Imaging.RoundedCornerLayer(5));
Assert.AreNotEqual(original, imageFactory.Image);
}
}

2
src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs

@ -311,7 +311,7 @@ namespace ImageProcessor.Web.UnitTests
Dictionary<string, RoundedCornerLayer> data = new Dictionary<string, RoundedCornerLayer>
{
{
"roundedcorners=30", new RoundedCornerLayer(30, true, true, true, true)
"roundedcorners=30", new RoundedCornerLayer(30)
},
{
"roundedcorners=radius-26|tl-true|tr-false|bl-true|br-false", new RoundedCornerLayer(26, true, false, true, false)

12
src/ImageProcessor.Web/Processors/RoundedCorners.cs

@ -14,7 +14,6 @@ namespace ImageProcessor.Web.Processors
using System.Text.RegularExpressions;
using ImageProcessor.Imaging;
using ImageProcessor.Processors;
using ImageProcessor.Web.Helpers;
/// <summary>
/// Encapsulates methods to add rounded corners to an image.
@ -104,12 +103,11 @@ namespace ImageProcessor.Web.Processors
this.SortOrder = match.Index;
RoundedCornerLayer roundedCornerLayer = new RoundedCornerLayer(
this.ParseRadius(queryString),
CommonParameterParserUtility.ParseColor(queryString),
this.ParseCorner(TopLeftRegex, queryString),
this.ParseCorner(TopRightRegex, queryString),
this.ParseCorner(BottomLeftRegex, queryString),
this.ParseCorner(BottomRightRegex, queryString));
this.ParseRadius(queryString),
this.ParseCorner(TopLeftRegex, queryString),
this.ParseCorner(TopRightRegex, queryString),
this.ParseCorner(BottomLeftRegex, queryString),
this.ParseCorner(BottomRightRegex, queryString));
this.Processor.DynamicParameter = roundedCornerLayer;
}

61
src/ImageProcessor/Imaging/RoundedCornerLayer.cs

@ -10,65 +10,17 @@
namespace ImageProcessor.Imaging
{
using System.Drawing;
/// <summary>
/// Encapsulates the properties required to add rounded corners to an image.
/// </summary>
public class RoundedCornerLayer
{
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RoundedCornerLayer"/> class.
/// </summary>
public RoundedCornerLayer()
{
this.Radius = 10;
this.BackgroundColor = Color.Transparent;
this.TopLeft = true;
this.TopRight = true;
this.BottomLeft = true;
this.BottomRight = true;
}
/// <summary>
/// Initializes a new instance of the <see cref="RoundedCornerLayer"/> class.
/// </summary>
/// <param name="radius">
/// The radius at which the corner will be done.
/// </param>
/// <param name="topLeft">
/// Set if top left is rounded
/// </param>
/// <param name="topRight">
/// Set if top right is rounded
/// </param>
/// <param name="bottomLeft">
/// Set if bottom left is rounded
/// </param>
/// <param name="bottomRight">
/// Set if bottom right is rounded
/// </param>
public RoundedCornerLayer(int radius, bool topLeft, bool topRight, bool bottomLeft, bool bottomRight)
{
this.Radius = radius;
this.BackgroundColor = Color.Transparent;
this.TopLeft = topLeft;
this.TopRight = topRight;
this.BottomLeft = bottomLeft;
this.BottomRight = bottomRight;
}
/// <summary>
/// Initializes a new instance of the <see cref="RoundedCornerLayer"/> class.
/// </summary>
/// <param name="radius">
/// The radius at which the corner will be done.
/// </param>
/// <param name="backgroundColor">
/// The <see cref="T:System.Drawing.Color"/> to set as the background color.
/// <remarks>Used for image formats that do not support transparency</remarks>
/// </param>
/// <param name="topLeft">
/// Set if top left is rounded
/// </param>
@ -81,16 +33,14 @@ namespace ImageProcessor.Imaging
/// <param name="bottomRight">
/// Set if bottom right is rounded
/// </param>
public RoundedCornerLayer(int radius, Color backgroundColor, bool topLeft, bool topRight, bool bottomLeft, bool bottomRight)
public RoundedCornerLayer(int radius, bool topLeft = true, bool topRight = true, bool bottomLeft = true, bool bottomRight = true)
{
this.Radius = radius;
this.BackgroundColor = backgroundColor;
this.TopLeft = topLeft;
this.TopRight = topRight;
this.BottomLeft = bottomLeft;
this.BottomRight = bottomRight;
}
#endregion
#region Properties
/// <summary>
@ -98,11 +48,6 @@ namespace ImageProcessor.Imaging
/// </summary>
public int Radius { get; set; }
/// <summary>
/// Gets or sets the background color.
/// </summary>
public Color BackgroundColor { get; set; }
/// <summary>
/// Gets or sets a value indicating whether top left corners are to be added.
/// </summary>
@ -145,7 +90,7 @@ namespace ImageProcessor.Imaging
return false;
}
return this.Radius == rounded.Radius && this.BackgroundColor == rounded.BackgroundColor
return this.Radius == rounded.Radius
&& this.TopLeft == rounded.TopLeft && this.TopRight == rounded.TopRight
&& this.BottomLeft == rounded.BottomLeft && this.BottomRight == rounded.BottomRight;
}
@ -158,7 +103,7 @@ namespace ImageProcessor.Imaging
/// </returns>
public override int GetHashCode()
{
return this.Radius.GetHashCode() + this.BackgroundColor.GetHashCode() +
return this.Radius.GetHashCode() +
this.TopLeft.GetHashCode() + this.TopRight.GetHashCode() +
this.BottomLeft.GetHashCode() + this.BottomRight.GetHashCode();
}

13
src/ImageProcessor/Processors/RoundedCorners.cs

@ -68,14 +68,13 @@ namespace ImageProcessor.Processors
{
RoundedCornerLayer roundedCornerLayer = this.DynamicParameter;
int radius = roundedCornerLayer.Radius;
Color backgroundColor = roundedCornerLayer.BackgroundColor;
bool topLeft = roundedCornerLayer.TopLeft;
bool topRight = roundedCornerLayer.TopRight;
bool bottomLeft = roundedCornerLayer.BottomLeft;
bool bottomRight = roundedCornerLayer.BottomRight;
// Create a rounded image.
newImage = this.RoundCornerImage(image, radius, backgroundColor, topLeft, topRight, bottomLeft, bottomRight);
newImage = this.RoundCornerImage(image, radius, topLeft, topRight, bottomLeft, bottomRight);
image.Dispose();
image = newImage;
@ -98,13 +97,12 @@ namespace ImageProcessor.Processors
/// </summary>
/// <param name="image">The image to add corners too</param>
/// <param name="cornerRadius">The radius of the corners.</param>
/// <param name="backgroundColor">The background color to fill an image with.</param>
/// <param name="topLeft">If the top left corner will have a rounded corner?</param>
/// <param name="topRight">If the top right corner will have a rounded corner?</param>
/// <param name="bottomLeft">If the bottom left corner will have a rounded corner?</param>
/// <param name="bottomRight">If the bottom right corner will have a rounded corner?</param>
/// <returns>The image with rounded corners.</returns>
private Bitmap RoundCornerImage(Image image, int cornerRadius, Color backgroundColor, bool topLeft = false, bool topRight = false, bool bottomLeft = false, bool bottomRight = false)
private Bitmap RoundCornerImage(Image image, int cornerRadius, bool topLeft = false, bool topRight = false, bool bottomLeft = false, bool bottomRight = false)
{
int width = image.Width;
int height = image.Height;
@ -119,14 +117,9 @@ namespace ImageProcessor.Processors
{
// Reduce the jagged edge.
graphics.SmoothingMode = SmoothingMode.HighQuality;
// Contrary to everything I have read bicubic is producing the best results.
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighSpeed;
// Fill the background.
graphics.Clear(backgroundColor);
graphics.CompositingQuality = CompositingQuality.HighQuality;
// Add rounded corners
using (GraphicsPath path = new GraphicsPath())

Loading…
Cancel
Save