2013-11-11 13:48:31 +13:00
#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
{
2013-11-11 14:49:53 +13:00
[DefaultValue(PositionType.Bottom_Right), TypeConverter(typeof(EnumDescriptionConverter))]
2013-11-11 13:48:31 +13:00
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 ; }
2013-11-12 12:32:16 +13:00
private FontSafe textFontSafe = new FontSafe ( ) ;
// Workaround for "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
2013-11-12 15:42:10 +13:00
[DefaultValue(typeof(Font), "Arial, 10pt")]
2013-11-12 12:32:16 +13:00
public Font TextFont
{
get
{
return textFontSafe . GetFont ( ) ;
}
set
{
using ( value )
{
textFontSafe . SetFont ( value ) ;
}
}
}
2013-11-11 13:48:31 +13:00
[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 ; }
2013-11-12 15:42:10 +13:00
[DefaultValue(typeof(Color), "100, 100, 100"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))]
2013-11-11 13:48:31 +13:00
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 )
{
2013-11-12 12:32:16 +13:00
if ( string . IsNullOrEmpty ( Text ) )
2013-11-11 19:28:23 +13:00
{
return img ;
}
2013-11-12 12:32:16 +13:00
using ( Font textFont = TextFont )
2013-11-11 13:48:31 +13:00
{
2013-11-12 12:32:16 +13:00
if ( textFont = = null | | textFont . Size < 1 )
{
return img ;
}
2013-11-11 13:48:31 +13:00
2013-11-12 12:32:16 +13:00
NameParser parser = new NameParser ( NameParserType . Text ) { Picture = img } ;
string parsedText = parser . Parse ( Text ) ;
2013-11-11 13:48:31 +13:00
2013-11-12 12:32:16 +13:00
Size textSize = TextRenderer . MeasureText ( parsedText , textFont ) ;
Size labelSize = new Size ( textSize . Width + BackgroundPadding * 2 , textSize . Height + BackgroundPadding * 2 ) ;
2013-11-11 13:48:31 +13:00
2013-11-12 12:32:16 +13:00
if ( AutoHide & & ( ( labelSize . Width + Offset > img . Width ) | | ( labelSize . Height + Offset > img . Height ) ) )
2013-11-11 13:48:31 +13:00
{
2013-11-12 12:32:16 +13:00
return img ;
}
2013-11-11 13:48:31 +13:00
2013-11-12 12:32:16 +13:00
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 ;
2013-11-11 13:48:31 +13:00
2013-11-12 12:32:16 +13:00
if ( DrawBackground )
{
using ( GraphicsPath gPath = new GraphicsPath ( ) )
2013-11-11 13:48:31 +13:00
{
2013-11-12 12:32:16 +13:00
gPath . AddRoundedRectangle ( labelRectangle , CornerRadius ) ;
Brush backgroundBrush = null ;
try
2013-11-11 13:48:31 +13:00
{
2013-11-12 12:32:16 +13:00
if ( UseGradient )
{
backgroundBrush = new LinearGradientBrush ( labelRectangle , BackgroundColor , BackgroundColor2 , GradientType ) ;
}
else
{
backgroundBrush = new SolidBrush ( BackgroundColor ) ;
}
g . FillPath ( backgroundBrush , gPath ) ;
2013-11-11 13:48:31 +13:00
}
2013-11-12 12:32:16 +13:00
finally
2013-11-11 13:48:31 +13:00
{
2013-11-12 12:32:16 +13:00
if ( backgroundBrush ! = null ) backgroundBrush . Dispose ( ) ;
2013-11-11 13:48:31 +13:00
}
2013-11-12 12:32:16 +13:00
using ( Pen borderPen = new Pen ( BorderColor ) )
{
g . DrawPath ( borderPen , gPath ) ;
}
2013-11-11 13:48:31 +13:00
}
}
2013-11-12 12:32:16 +13:00
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 ) ;
}
2013-11-11 13:48:31 +13:00
2013-11-12 12:32:16 +13:00
using ( Graphics gImg = Graphics . FromImage ( img ) )
{
gImg . SetHighQuality ( ) ;
2013-11-11 13:48:31 +13:00
2013-11-12 12:32:16 +13:00
Point labelPosition = Helpers . GetPosition ( Position , Offset , img . Size , labelSize ) ;
gImg . DrawImage ( bmp , labelPosition . X , labelPosition . Y , bmp . Width , bmp . Height ) ;
}
2013-11-11 13:48:31 +13:00
}
}
return img ;
}
}
}