Added DrawText image effect (Text watermark)

This commit is contained in:
Jaex 2013-11-11 02:48:31 +02:00
parent a450d30972
commit a16428d869
8 changed files with 187 additions and 16 deletions

View file

@ -33,24 +33,32 @@ namespace HelpersLib
{
public static class GraphicsPathExtensions
{
public static void AddRectangleProper(this GraphicsPath graphicsPath, RectangleF rect)
public static void AddRectangleProper(this GraphicsPath graphicsPath, RectangleF rect, float penWidth = 1)
{
rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
if (penWidth == 1)
{
rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
}
if (rect.Width > 1 && rect.Height > 1)
if (rect.Width > 0 && rect.Height > 0)
{
graphicsPath.AddRectangle(rect);
}
}
public static void AddRoundedRectangle(this GraphicsPath graphicsPath, RectangleF rect, float radius)
public static void AddRoundedRectangle(this GraphicsPath graphicsPath, RectangleF rect, float radius, float penWidth = 1)
{
if (radius <= 0.0f)
if (radius <= 0f)
{
graphicsPath.AddRectangle(rect);
graphicsPath.AddRectangleProper(rect);
}
else
{
if (penWidth == 1)
{
rect = new RectangleF(rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
}
// If the corner radius is greater than or equal to
// half the width, or height (whichever is shorter)
// then return a capsule instead of a lozenge

View file

@ -38,15 +38,18 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide
{
return base.EditValue(context, provider, value);
}
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Title = "Browse for images...";
dlg.Filter = "Image files (*.jpg, *.jpeg, *.png, *.gif, *.bmp)|*.jpg;*.jpeg;*.png;*.gif;*.bmp";
if (dlg.ShowDialog() == DialogResult.OK)
{
value = dlg.FileName;
}
}
return value;
}
}

View file

@ -41,9 +41,9 @@ internal class DrawBackground : ImageEffect
public bool UseGradient { get; set; }
[DefaultValue(typeof(Color), "White"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color ToColor { get; set; }
public Color Color2 { get; set; }
[DefaultValue(LinearGradientMode.ForwardDiagonal)]
[DefaultValue(LinearGradientMode.Vertical)]
public LinearGradientMode GradientType { get; set; }
public DrawBackground()
@ -55,7 +55,7 @@ public override Image Apply(Image img)
{
if (UseGradient)
{
return ImageHelpers.FillBackground(img, Color, ToColor, GradientType);
return ImageHelpers.FillBackground(img, Color, Color2, GradientType);
}
else
{

View file

@ -47,9 +47,9 @@ internal class DrawBorder : ImageEffect
public bool UseGradient { get; set; }
[DefaultValue(typeof(Color), "White"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color ToColor { get; set; }
public Color Color2 { get; set; }
[DefaultValue(LinearGradientMode.ForwardDiagonal)]
[DefaultValue(LinearGradientMode.Vertical)]
public LinearGradientMode GradientType { get; set; }
public DrawBorder()
@ -61,7 +61,7 @@ public override Image Apply(Image img)
{
if (UseGradient)
{
return ImageHelpers.DrawBorder(img, Color, ToColor, GradientType, Size, Type);
return ImageHelpers.DrawBorder(img, Color, Color2, GradientType, Size, Type);
}
else
{

View file

@ -35,9 +35,6 @@ namespace ImageEffectsLib
[Description("Image")]
internal class DrawImage : ImageEffect
{
[DefaultValue(""), Editor(typeof(ImageFileNameEditor), typeof(UITypeEditor))]
public string ImageLocation { get; set; }
[DefaultValue(PositionType.Bottom_Right)]
public PositionType Position { get; set; }
@ -47,6 +44,9 @@ internal class DrawImage : ImageEffect
[DefaultValue(false), Description("If image size bigger than source image then don't draw it.")]
public bool AutoHide { get; set; }
[DefaultValue(""), Editor(typeof(ImageFileNameEditor), typeof(UITypeEditor))]
public string ImageLocation { get; set; }
public DrawImage()
{
this.ApplyDefaultPropertyValues();

View file

@ -0,0 +1,158 @@
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (C) 2008-2013 ShareX Developers
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using HelpersLib;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
using System.Windows.Forms;
namespace ImageEffectsLib
{
[Description("Text")]
internal class DrawText : ImageEffect
{
[DefaultValue(PositionType.Bottom_Right)]
public PositionType Position { get; set; }
[DefaultValue(5)]
public int Offset { get; set; }
[DefaultValue(false), Description("If watermark size bigger than source image then don't draw it.")]
public bool AutoHide { get; set; }
[DefaultValue("%h:%mi")]
public string Text { get; set; }
[DefaultValue(typeof(Font), "Arial, 8pt")]
public Font TextFont { get; set; }
[DefaultValue(typeof(Color), "White"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color TextColor { get; set; }
[DefaultValue(true)]
public bool DrawBackground { get; set; }
[DefaultValue(5)]
public int BackgroundPadding { get; set; }
[DefaultValue(4)]
public int CornerRadius { get; set; }
[DefaultValue(typeof(Color), "Black"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color BorderColor { get; set; }
[DefaultValue(typeof(Color), "85, 85, 85"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color BackgroundColor { get; set; }
[DefaultValue(true)]
public bool UseGradient { get; set; }
[DefaultValue(typeof(Color), "Black"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
public Color BackgroundColor2 { get; set; }
[DefaultValue(LinearGradientMode.Vertical)]
public LinearGradientMode GradientType { get; set; }
public DrawText()
{
this.ApplyDefaultPropertyValues();
}
public override Image Apply(Image img)
{
NameParser parser = new NameParser(NameParserType.Text) { Picture = img };
string parsedText = parser.Parse(Text);
Size textSize = TextRenderer.MeasureText(parsedText, TextFont);
Size labelSize = new Size(textSize.Width + BackgroundPadding * 2, textSize.Height + BackgroundPadding * 2);
if (AutoHide && ((labelSize.Width + Offset > img.Width) || (labelSize.Height + Offset > img.Height)))
{
return img;
}
Rectangle labelRectangle = new Rectangle(Point.Empty, labelSize);
using (Bitmap bmp = new Bitmap(labelRectangle.Width, labelRectangle.Height))
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
if (DrawBackground)
{
using (GraphicsPath gPath = new GraphicsPath())
{
gPath.AddRoundedRectangle(labelRectangle, CornerRadius);
Brush backgroundBrush = null;
try
{
if (UseGradient)
{
backgroundBrush = new LinearGradientBrush(labelRectangle, BackgroundColor, BackgroundColor2, GradientType);
}
else
{
backgroundBrush = new SolidBrush(BackgroundColor);
}
g.FillPath(backgroundBrush, gPath);
}
finally
{
if (backgroundBrush != null) backgroundBrush.Dispose();
}
using (Pen borderPen = new Pen(BorderColor))
{
g.DrawPath(borderPen, gPath);
}
}
}
using (Brush textBrush = new SolidBrush(TextColor))
using (StringFormat sf = new StringFormat { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Center })
{
g.DrawString(parsedText, TextFont, textBrush, bmp.Width / 2f, bmp.Height / 2f, sf);
}
using (Graphics gImg = Graphics.FromImage(img))
{
gImg.SetHighQuality();
Point labelPosition = Helpers.GetPosition(Position, Offset, img.Size, labelSize);
gImg.DrawImage(bmp, labelPosition.X, labelPosition.Y, bmp.Width, bmp.Height);
}
}
return img;
}
}
}

View file

@ -67,7 +67,8 @@ private void AddAllEffectsToTreeView()
AddEffectToTreeView("Drawings",
typeof(DrawBackground),
typeof(DrawBorder),
typeof(DrawImage));
typeof(DrawImage),
typeof(DrawText));
AddEffectToTreeView("Manipulations",
typeof(Crop),

View file

@ -77,6 +77,7 @@
<Compile Include="Adjustments\Sepia.cs" />
<Compile Include="Adjustments\MatrixColor.cs" />
<Compile Include="Drawings\DrawImage.cs" />
<Compile Include="Drawings\DrawWatermark.cs" />
<Compile Include="Filters\EdgeDetect.cs" />
<Compile Include="Filters\Emboss.cs" />
<Compile Include="Filters\GaussianBlur.cs" />