Added Sides setting to Torn Edge effect

This commit is contained in:
Jaex 2013-11-28 09:22:06 +02:00
parent 427873caa0
commit 0790fbe035
2 changed files with 69 additions and 26 deletions

View file

@ -777,7 +777,7 @@ public static Bitmap Pixelate(Bitmap sourceImage, int pixelSize)
return result;
}
public static Image CreateTornEdge(Image sourceImage, int toothHeight, int horizontalToothRange, int verticalToothRange)
public static Image CreateTornEdge(Image sourceImage, int toothHeight, int horizontalToothRange, int verticalToothRange, AnchorStyles sides)
{
Image result = sourceImage.CreateEmptyBitmap(PixelFormat.Format32bppArgb);
@ -787,53 +787,87 @@ public static Image CreateTornEdge(Image sourceImage, int toothHeight, int horiz
int horizontalRegions = sourceImage.Width / horizontalToothRange;
int verticalRegions = sourceImage.Height / verticalToothRange;
// Start
Point previousEndingPoint = new Point(horizontalToothRange, random.Next(1, toothHeight));
Point newEndingPoint;
// Top
for (int i = 0; i < horizontalRegions; i++)
if (sides.HasFlag(AnchorStyles.Top))
{
int x = previousEndingPoint.X + horizontalToothRange;
int y = random.Next(1, toothHeight);
newEndingPoint = new Point(x, y);
for (int i = 0; i < horizontalRegions; i++)
{
int x = previousEndingPoint.X + horizontalToothRange;
int y = random.Next(1, toothHeight);
newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
}
else
{
previousEndingPoint = new Point(0, 0);
newEndingPoint = new Point(sourceImage.Width, 0);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
// Right
for (int i = 0; i < verticalRegions; i++)
if (sides.HasFlag(AnchorStyles.Right))
{
int x = sourceImage.Width - random.Next(1, toothHeight);
int y = previousEndingPoint.Y + verticalToothRange;
newEndingPoint = new Point(x, y);
for (int i = 0; i < verticalRegions; i++)
{
int x = sourceImage.Width - random.Next(1, toothHeight);
int y = previousEndingPoint.Y + verticalToothRange;
newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
}
else
{
previousEndingPoint = new Point(sourceImage.Width, 0);
newEndingPoint = new Point(sourceImage.Width, sourceImage.Height);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
// Bottom
for (int i = 0; i < horizontalRegions; i++)
if (sides.HasFlag(AnchorStyles.Bottom))
{
int x = previousEndingPoint.X - horizontalToothRange;
int y = sourceImage.Height - random.Next(1, toothHeight);
newEndingPoint = new Point(x, y);
for (int i = 0; i < horizontalRegions; i++)
{
int x = previousEndingPoint.X - horizontalToothRange;
int y = sourceImage.Height - random.Next(1, toothHeight);
newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
}
else
{
previousEndingPoint = new Point(sourceImage.Width, sourceImage.Height);
newEndingPoint = new Point(0, sourceImage.Height);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
// Left
for (int i = 0; i < verticalRegions; i++)
if (sides.HasFlag(AnchorStyles.Left))
{
int x = random.Next(1, toothHeight);
int y = previousEndingPoint.Y - verticalToothRange;
newEndingPoint = new Point(x, y);
for (int i = 0; i < verticalRegions; i++)
{
int x = random.Next(1, toothHeight);
int y = previousEndingPoint.Y - verticalToothRange;
newEndingPoint = new Point(x, y);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
}
else
{
previousEndingPoint = new Point(0, sourceImage.Height);
newEndingPoint = new Point(0, 0);
path.AddLine(previousEndingPoint, newEndingPoint);
previousEndingPoint = newEndingPoint;
}
path.CloseFigure();
// Draw the created figure with the original image by using a TextureBrush so we have anti-aliasing
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.SmoothingMode = SmoothingMode.HighQuality;
@ -841,9 +875,9 @@ public static Image CreateTornEdge(Image sourceImage, int toothHeight, int horiz
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
// Draw the created figure with the original image by using a TextureBrush so we have anti-aliasing
using (Brush brush = new TextureBrush(sourceImage))
{
// Imporant note: If the target wouldn't be at 0,0 we need to translate-transform!!
graphics.FillPath(brush, path);
}
}

View file

@ -26,6 +26,7 @@
using HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace ImageEffectsLib
{
@ -41,6 +42,9 @@ internal class TornEdge : ImageEffect
[DefaultValue(20)]
public int VerticalToothRange { get; set; }
[DefaultValue(AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right)]
public AnchorStyles Sides { get; set; }
public TornEdge()
{
this.ApplyDefaultPropertyValues();
@ -48,10 +52,15 @@ public TornEdge()
public override Image Apply(Image img)
{
using (img)
if (Sides != AnchorStyles.None)
{
return ImageHelpers.CreateTornEdge(img, ToothHeight, HorizontalToothRange, VerticalToothRange);
using (img)
{
return ImageHelpers.CreateTornEdge(img, ToothHeight, HorizontalToothRange, VerticalToothRange, Sides);
}
}
return img;
}
}
}