Refactor effect shapes

This commit is contained in:
Jaex 2017-11-16 16:00:11 +03:00
parent b73ac79e4d
commit 5aefb10de4
4 changed files with 45 additions and 111 deletions

View file

@ -23,6 +23,7 @@ You should have received a copy of the GNU General Public License
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System.Drawing;
using System.Drawing.Drawing2D;
@ -32,6 +33,8 @@ public abstract class BaseEffectShape : BaseShape
{
public override ShapeCategory ShapeCategory { get; } = ShapeCategory.Effect;
public abstract string OverlayText { get; }
private Image cachedEffect;
public abstract void ApplyEffect(Bitmap bmp);
@ -50,11 +53,43 @@ public virtual void OnDraw(Graphics g)
}
}
public abstract void OnDrawOverlay(Graphics g);
public virtual void OnDrawOverlay(Graphics g)
{
using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black)))
{
g.FillRectangle(brush, Rectangle);
}
g.DrawCornerLines(Rectangle.Offset(1), Pens.White, 25);
using (Font font = new Font("Verdana", 12))
{
string text = OverlayText;
Size textSize = g.MeasureString(text, font).ToSize();
if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height)
{
using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
g.DrawString(text, font, Brushes.White, Rectangle, sf);
}
}
}
}
public virtual void OnDrawFinal(Graphics g, Bitmap bmp)
{
OnDraw(g);
Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle);
if (!rect.IsEmpty)
{
using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect))
{
ApplyEffect(croppedImage);
g.DrawImage(croppedImage, rect);
}
}
}
public override void OnCreated()

View file

@ -32,6 +32,8 @@ public class BlurEffectShape : BaseEffectShape
{
public override ShapeType ShapeType { get; } = ShapeType.EffectBlur;
public override string OverlayText => $"Blur [{BlurRadius}]";
public int BlurRadius { get; set; }
public override void OnConfigLoad()
@ -49,45 +51,11 @@ public override void ApplyEffect(Bitmap bmp)
ImageHelpers.BoxBlur(bmp, BlurRadius);
}
public override void OnDrawOverlay(Graphics g)
{
using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black)))
{
g.FillRectangle(brush, Rectangle);
}
g.DrawCornerLines(Rectangle.Offset(1), Pens.White, 20);
using (Font font = new Font("Verdana", 12))
{
string text = $"Blur ({BlurRadius})";
Size textSize = g.MeasureString(text, font).ToSize();
if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height)
{
using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
g.DrawString(text, font, Brushes.White, Rectangle, sf);
}
}
}
}
public override void OnDrawFinal(Graphics g, Bitmap bmp)
{
if (BlurRadius > 1)
{
Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle);
if (!rect.IsEmpty)
{
using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect))
{
ApplyEffect(croppedImage);
g.DrawImage(croppedImage, rect);
}
}
base.OnDrawFinal(g, bmp);
}
}
}

View file

@ -32,6 +32,8 @@ public class HighlightEffectShape : BaseEffectShape
{
public override ShapeType ShapeType { get; } = ShapeType.EffectHighlight;
public override string OverlayText => "Highlight";
public Color HighlightColor { get; set; }
public override void OnConfigLoad()
@ -48,44 +50,5 @@ public override void ApplyEffect(Bitmap bmp)
{
ImageHelpers.HighlightImage(bmp, HighlightColor);
}
public override void OnDrawOverlay(Graphics g)
{
using (Brush brush = new SolidBrush(Color.FromArgb(100, HighlightColor)))
{
g.FillRectangle(brush, Rectangle);
}
g.DrawCornerLines(Rectangle.Offset(1), Pens.Black, 20);
using (Font font = new Font("Verdana", 12))
{
string text = "Highlight";
Size textSize = g.MeasureString(text, font).ToSize();
if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height)
{
using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
g.DrawString(text, font, Brushes.Black, Rectangle, sf);
}
}
}
}
public override void OnDrawFinal(Graphics g, Bitmap bmp)
{
Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle);
if (!rect.IsEmpty)
{
using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect))
{
ApplyEffect(croppedImage);
g.DrawImage(croppedImage, rect);
}
}
}
}
}

View file

@ -32,6 +32,8 @@ public class PixelateEffectShape : BaseEffectShape
{
public override ShapeType ShapeType { get; } = ShapeType.EffectPixelate;
public override string OverlayText => $"Pixelate [{PixelSize}]";
public int PixelSize { get; set; }
public override void OnConfigLoad()
@ -49,45 +51,11 @@ public override void ApplyEffect(Bitmap bmp)
ImageHelpers.Pixelate(bmp, PixelSize);
}
public override void OnDrawOverlay(Graphics g)
{
using (Brush brush = new SolidBrush(Color.FromArgb(150, Color.Black)))
{
g.FillRectangle(brush, Rectangle);
}
g.DrawCornerLines(Rectangle.Offset(1), Pens.White, 20);
using (Font font = new Font("Verdana", 12))
{
string text = $"Pixelate ({PixelSize})";
Size textSize = g.MeasureString(text, font).ToSize();
if (Rectangle.Width > textSize.Width && Rectangle.Height > textSize.Height)
{
using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
g.DrawString(text, font, Brushes.White, Rectangle, sf);
}
}
}
}
public override void OnDrawFinal(Graphics g, Bitmap bmp)
{
if (PixelSize > 1)
{
Rectangle rect = Rectangle.Intersect(new Rectangle(0, 0, bmp.Width, bmp.Height), Rectangle);
if (!rect.IsEmpty)
{
using (Bitmap croppedImage = ImageHelpers.CropBitmap(bmp, rect))
{
ApplyEffect(croppedImage);
g.DrawImage(croppedImage, rect);
}
}
base.OnDrawFinal(g, bmp);
}
}
}