mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Added the ability to generate watermarks and performed a general tidy up. Former-commit-id: aa7a8fca6d9406d7f5cec6ead9ede3e77ee88ff6af/merge-core
15 changed files with 1074 additions and 288 deletions
@ -1,103 +1,105 @@ |
|||||
<?xml version="1.0" encoding="utf-8"?> |
<?xml version="1.0" encoding="utf-8"?> |
||||
<configuration> |
<configuration> |
||||
<!-- VSdocman config file for current project/solution.--> |
<!-- VSdocman config file for current project/solution.--> |
||||
<activeProfile>default</activeProfile> |
<activeProfile>default</activeProfile> |
||||
<appSettings> |
<appSettings> |
||||
<add key="VBdocman_regexFilters"><![CDATA[<?xml version="1.0" encoding="utf-8"?> |
<add key="VBdocman_regexFilters"><![CDATA[<?xml version="1.0" encoding="utf-8"?> |
||||
<filters />]]></add> |
<filters />]]></add> |
||||
<add key="VBdocman_comNonCommented"><![CDATA[-1]]></add> |
<add key="VBdocman_comNonCommented"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_comPublic"><![CDATA[-1]]></add> |
<add key="VBdocman_comPublic"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_comPrivate"><![CDATA[0]]></add> |
<add key="VBdocman_comPrivate"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comFriend"><![CDATA[0]]></add> |
<add key="VBdocman_comFriend"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comProtected"><![CDATA[0]]></add> |
<add key="VBdocman_comProtected"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comProtectedFriend"><![CDATA[0]]></add> |
<add key="VBdocman_comProtectedFriend"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comMethod"><![CDATA[-1]]></add> |
<add key="VBdocman_comMethod"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_comStdModule"><![CDATA[0]]></add> |
<add key="VBdocman_comStdModule"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comObject"><![CDATA[-1]]></add> |
<add key="VBdocman_comObject"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_comForm"><![CDATA[-1]]></add> |
<add key="VBdocman_comForm"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_comProperty"><![CDATA[-1]]></add> |
<add key="VBdocman_comProperty"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_comEvent"><![CDATA[-1]]></add> |
<add key="VBdocman_comEvent"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_comVariable"><![CDATA[0]]></add> |
<add key="VBdocman_comVariable"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comConstant"><![CDATA[0]]></add> |
<add key="VBdocman_comConstant"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comEnumeration"><![CDATA[0]]></add> |
<add key="VBdocman_comEnumeration"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comStructure"><![CDATA[0]]></add> |
<add key="VBdocman_comStructure"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comDelegate"><![CDATA[0]]></add> |
<add key="VBdocman_comDelegate"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comInterface"><![CDATA[0]]></add> |
<add key="VBdocman_comInterface"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comAttribute"><![CDATA[0]]></add> |
<add key="VBdocman_comAttribute"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comEventDecl"><![CDATA[0]]></add> |
<add key="VBdocman_comEventDecl"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comDeclare"><![CDATA[0]]></add> |
<add key="VBdocman_comDeclare"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comContextID"><![CDATA[0]]></add> |
<add key="VBdocman_comContextID"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comWriteDescription"><![CDATA[-1]]></add> |
<add key="VBdocman_comWriteDescription"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_useConditionalCompilation"><![CDATA[0]]></add> |
<add key="VBdocman_useConditionalCompilation"><![CDATA[0]]></add> |
||||
<add key="VBdocman_conditionalConstants"><![CDATA[]]></add> |
<add key="VBdocman_conditionalConstants"><![CDATA[]]></add> |
||||
<add key="VBdocman_showFormsSeparate"><![CDATA[0]]></add> |
<add key="VBdocman_showFormsSeparate"><![CDATA[0]]></add> |
||||
<add key="VBdocman_showInherited"><![CDATA[-1]]></add> |
<add key="VBdocman_showInherited"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_indentMode"><![CDATA[0]]></add> |
<add key="VBdocman_indentMode"><![CDATA[0]]></add> |
||||
<add key="VBdocman_insertSourceGlobal"><![CDATA[0]]></add> |
<add key="VBdocman_insertSourceGlobal"><![CDATA[0]]></add> |
||||
<add key="VBdocman_unbreakSourceLines"><![CDATA[0]]></add> |
<add key="VBdocman_unbreakSourceLines"><![CDATA[0]]></add> |
||||
<add key="VBdocman_removeAttributes"><![CDATA[0]]></add> |
<add key="VBdocman_removeAttributes"><![CDATA[0]]></add> |
||||
<add key="VBdocman_generateVbSyntax"><![CDATA[-1]]></add> |
<add key="VBdocman_generateVbSyntax"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_generateCsharpSyntax"><![CDATA[-1]]></add> |
<add key="VBdocman_generateCsharpSyntax"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_generateCppSyntax"><![CDATA[-1]]></add> |
<add key="VBdocman_generateCppSyntax"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_generateJscriptSyntax"><![CDATA[-1]]></add> |
<add key="VBdocman_generateJscriptSyntax"><![CDATA[-1]]></add> |
||||
<add key="VBdocman_supportedPlatforms"><![CDATA[Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition]]></add> |
<add key="VBdocman_supportedPlatforms"><![CDATA[Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition]]></add> |
||||
<add key="VBdocman_supportedNetFramework"><![CDATA[3.0, 2.0, 1.1, 1.0]]></add> |
<add key="VBdocman_supportedNetFramework"><![CDATA[3.0, 2.0, 1.1, 1.0]]></add> |
||||
<add key="VBdocman_supportedNetCompactFramework"><![CDATA[2.0, 1.0]]></add> |
<add key="VBdocman_supportedNetCompactFramework"><![CDATA[2.0, 1.0]]></add> |
||||
<add key="VBdocman_supportedXnaFramework"><![CDATA[1.0]]></add> |
<add key="VBdocman_supportedXnaFramework"><![CDATA[1.0]]></add> |
||||
<add key="VBdocman_customVar1"><![CDATA[]]></add> |
<add key="VBdocman_customVar1"><![CDATA[]]></add> |
||||
<add key="VBdocman_customVar2"><![CDATA[]]></add> |
<add key="VBdocman_customVar2"><![CDATA[]]></add> |
||||
<add key="VBdocman_customVar3"><![CDATA[]]></add> |
<add key="VBdocman_customVar3"><![CDATA[]]></add> |
||||
<add key="VBdocman_titlePageText"><![CDATA[]]></add> |
<add key="VBdocman_titlePageText"><![CDATA[]]></add> |
||||
<add key="VBdocman_rootNamespaceText"><![CDATA[]]></add> |
<add key="VBdocman_rootNamespaceText"><![CDATA[]]></add> |
||||
<add key="VBdocman_rootNamespaceCommentStyle"><![CDATA[2]]></add> |
<add key="VBdocman_rootNamespaceCommentStyle"><![CDATA[2]]></add> |
||||
<add key="VBdocman_pageFooterText"><![CDATA[Generated by VSdocman]]></add> |
<add key="VBdocman_pageFooterText"><![CDATA[Generated by VSdocman]]></add> |
||||
<add key="VBdocman_outputPath"><![CDATA[$(ProjectDir)VSdoc]]></add> |
<add key="VBdocman_outputPath"><![CDATA[$(ProjectDir)VSdoc]]></add> |
||||
<add key="VBdocman_templateFolder"><![CDATA[$(VSdocmanDir)Templates]]></add> |
<add key="VBdocman_templateFolder"><![CDATA[$(VSdocmanDir)Templates]]></add> |
||||
<add key="VBdocman_externalFilesFolder"><![CDATA[]]></add> |
<add key="VBdocman_externalFilesFolder"><![CDATA[]]></add> |
||||
<add key="VBdocman_fileNamingConvention"><![CDATA[1]]></add> |
<add key="VBdocman_fileNamingConvention"><![CDATA[1]]></add> |
||||
<add key="VBdocman_templateLocale"><![CDATA[en-US]]></add> |
<add key="VBdocman_templateLocale"><![CDATA[en-US]]></add> |
||||
<add key="VBdocman_templatePath"><![CDATA[chm_msdn2.vbdt]]></add> |
<add key="VBdocman_templatePath"><![CDATA[chm_msdn2.vbdt]]></add> |
||||
<add key="VBdocman_enumSorting"><![CDATA[1]]></add> |
<add key="VBdocman_enumSorting"><![CDATA[1]]></add> |
||||
<add key="VBdocman_helpTitle"><![CDATA[ImageProcessor Reference]]></add> |
<add key="VBdocman_helpTitle"><![CDATA[ImageProcessor Reference]]></add> |
||||
<add key="VBdocman_customTopics"><![CDATA[<?xml version="1.0" encoding="utf-8"?> |
<add key="VBdocman_customTopics"><![CDATA[<?xml version="1.0" encoding="utf-8"?> |
||||
<topics> |
<topics> |
||||
<topic> |
<topic> |
||||
<type>normal</type> |
<type>normal</type> |
||||
<is-default>yes</is-default> |
<is-default>yes</is-default> |
||||
<name>ImageProcessor Reference</name> |
<name>ImageProcessor Reference</name> |
||||
<id>imageprocessor_reference</id> |
<id>imageprocessor_reference</id> |
||||
<comment><![CDATA[<summary></summary>vsdocman_escaped_]_]_></comment> |
<comment><![CDATA[<summary></summary>vsdocman_escaped_]_]_></comment> |
||||
<namespaces /> |
<namespaces /> |
||||
<topics> |
<topics> |
||||
<topic> |
<topic> |
||||
<type>placeholder</type> |
<type>placeholder</type> |
||||
<is-default>no</is-default> |
<is-default>no</is-default> |
||||
<name /> |
<name /> |
||||
<id>e3cba20ec62e422b8bb39e6be7ca2142</id> |
<id>e3cba20ec62e422b8bb39e6be7ca2142</id> |
||||
<comment><![CDATA[vsdocman_escaped_]_]_></comment> |
<comment><![CDATA[vsdocman_escaped_]_]_></comment> |
||||
<namespaces /> |
<namespaces /> |
||||
<topics /> |
<topics /> |
||||
</topic> |
</topic> |
||||
</topics> |
</topics> |
||||
</topic> |
</topic> |
||||
</topics>]]></add> |
</topics>]]></add> |
||||
<add key="VBdocman_linkForExternalNotInFramework"><![CDATA[0]]></add> |
<add key="VBdocman_linkForExternalNotInFramework"><![CDATA[0]]></add> |
||||
<add key="VBdocman_allowMacrosInComments"><![CDATA[0]]></add> |
<add key="VBdocman_allowMacrosInComments"><![CDATA[0]]></add> |
||||
<add key="VBdocman_emptyOutputFolder"><![CDATA[0]]></add> |
<add key="VBdocman_emptyOutputFolder"><![CDATA[0]]></add> |
||||
<add key="VBdocman_comModules_Helpers...Extensions...EnumExtensions.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Helpers...Extensions...EnumExtensions.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Helpers...Extensions...StringExtensions.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Helpers...Extensions...StringExtensions.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_ImageFactory.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_ImageFactory.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Imaging...ImageUtils.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Imaging...ImageUtils.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Imaging...OctreeQuantizer.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Imaging...OctreeQuantizer.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Imaging...Quantizer.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Imaging...Quantizer.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Imaging...ResponseType.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Imaging...ResponseType.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Processors...Alpha.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Imaging...TextLayer.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Processors...Class1.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Processors...Alpha.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Processors...Crop.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Processors...Copy of Watermark.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Processors...Filter.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Processors...Crop.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Processors...Quality.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Processors...Filter.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Processors...Resize.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Processors...Quality.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Processors...Vignette.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Processors...Resize.cs"><![CDATA[On]]></add> |
||||
<add key="VBdocman_comModules_Properties...AssemblyInfo.cs"><![CDATA[On]]></add> |
<add key="VBdocman_comModules_Processors...Vignette.cs"><![CDATA[On]]></add> |
||||
</appSettings> |
<add key="VBdocman_comModules_Processors...Watermark.cs"><![CDATA[On]]></add> |
||||
|
<add key="VBdocman_comModules_Properties...AssemblyInfo.cs"><![CDATA[On]]></add> |
||||
|
</appSettings> |
||||
</configuration> |
</configuration> |
||||
@ -1,138 +0,0 @@ |
|||||
// -----------------------------------------------------------------------
|
|
||||
// <copyright file="PaletteQuantizer.cs" company="James South">
|
|
||||
// TODO: Update copyright text.
|
|
||||
// </copyright>
|
|
||||
// -----------------------------------------------------------------------
|
|
||||
|
|
||||
namespace ImageProcessor.Imaging |
|
||||
{ |
|
||||
using System; |
|
||||
using System.Collections; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Drawing; |
|
||||
using System.Drawing.Imaging; |
|
||||
using System.Linq; |
|
||||
using System.Text; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Encapsulates methods to calculate the colour palette if an image.
|
|
||||
/// http://codebetter.com/brendantompkins/2007/06/14/gif-image-color-quantizer-now-with-safe-goodness/
|
|
||||
/// </summary>
|
|
||||
internal class PaletteQuantizer : Quantizer |
|
||||
{ |
|
||||
/// <summary>
|
|
||||
/// Initializes a new instance of the <see cref="PaletteQuantizer"/> class.
|
|
||||
/// </summary>
|
|
||||
/// <param name="palette">
|
|
||||
/// The color palette to quantize to
|
|
||||
/// </param>
|
|
||||
/// <remarks>
|
|
||||
/// Palette quantization only requires a single quantization step
|
|
||||
/// </remarks>
|
|
||||
public PaletteQuantizer(ArrayList palette) |
|
||||
: base(true) |
|
||||
{ |
|
||||
_colorMap = new Hashtable(); |
|
||||
|
|
||||
_colors = new Color[palette.Count]; |
|
||||
palette.CopyTo(_colors); |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Override this to process the pixel in the second pass of the algorithm
|
|
||||
/// </summary>
|
|
||||
/// <param name="pixel">The pixel to quantize</param>
|
|
||||
/// <returns>The quantized value</returns>
|
|
||||
protected override byte QuantizePixel(Color32 pixel) |
|
||||
{ |
|
||||
byte colorIndex = 0; |
|
||||
int colorHash = pixel.ARGB; |
|
||||
|
|
||||
// Check if the color is in the lookup table
|
|
||||
if (_colorMap.ContainsKey(colorHash)) |
|
||||
{ |
|
||||
colorIndex = (byte)_colorMap[colorHash]; |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
// Not found - loop through the palette and find the nearest match.
|
|
||||
// Firstly check the alpha value - if 0, lookup the transparent color
|
|
||||
if (0 == pixel.Alpha) |
|
||||
{ |
|
||||
// Transparent. Lookup the first color with an alpha value of 0
|
|
||||
for (int index = 0; index < _colors.Length; index++) |
|
||||
{ |
|
||||
if (0 == _colors[index].A) |
|
||||
{ |
|
||||
colorIndex = (byte)index; |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
// Not transparent...
|
|
||||
int leastDistance = int.MaxValue; |
|
||||
int red = pixel.Red; |
|
||||
int green = pixel.Green; |
|
||||
int blue = pixel.Blue; |
|
||||
|
|
||||
// Loop through the entire palette, looking for the closest color match
|
|
||||
for (int index = 0; index < _colors.Length; index++) |
|
||||
{ |
|
||||
Color paletteColor = _colors[index]; |
|
||||
|
|
||||
int redDistance = paletteColor.R - red; |
|
||||
int greenDistance = paletteColor.G - green; |
|
||||
int blueDistance = paletteColor.B - blue; |
|
||||
|
|
||||
int distance = (redDistance * redDistance) + |
|
||||
(greenDistance * greenDistance) + |
|
||||
(blueDistance * blueDistance); |
|
||||
|
|
||||
if (distance < leastDistance) |
|
||||
{ |
|
||||
colorIndex = (byte)index; |
|
||||
leastDistance = distance; |
|
||||
|
|
||||
// And if it's an exact match, exit the loop
|
|
||||
if (0 == distance) |
|
||||
{ |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
// Now I have the color, pop it into the hashtable for next time
|
|
||||
_colorMap.Add(colorHash, colorIndex); |
|
||||
} |
|
||||
|
|
||||
return colorIndex; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Retrieve the palette for the quantized image
|
|
||||
/// </summary>
|
|
||||
/// <param name="palette">Any old palette, this is overrwritten</param>
|
|
||||
/// <returns>The new color palette</returns>
|
|
||||
protected override ColorPalette GetPalette(ColorPalette palette) |
|
||||
{ |
|
||||
for (int index = 0; index < _colors.Length; index++) |
|
||||
{ |
|
||||
palette.Entries[index] = _colors[index]; |
|
||||
} |
|
||||
return palette; |
|
||||
} |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// Lookup table for colors
|
|
||||
/// </summary>
|
|
||||
private Hashtable _colorMap; |
|
||||
|
|
||||
/// <summary>
|
|
||||
/// List of all colors in the palette
|
|
||||
/// </summary>
|
|
||||
protected Color[] _colors; |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,113 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="TextLayer.cs" company="James South">
|
||||
|
// TODO: Update copyright text.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace ImageProcessor.Imaging |
||||
|
{ |
||||
|
#region Using
|
||||
|
|
||||
|
using System; |
||||
|
using System.Drawing; |
||||
|
#endregion
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Enacapsulates the properties required to add a leyer of text to an image.
|
||||
|
/// </summary>
|
||||
|
public class TextLayer |
||||
|
{ |
||||
|
#region Fields
|
||||
|
/// <summary>
|
||||
|
/// The colour to render the text.
|
||||
|
/// </summary>
|
||||
|
private Color textColor = Color.Black; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The opacity at which to render the text.
|
||||
|
/// </summary>
|
||||
|
private int opacity = 100; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The font style to render the text.
|
||||
|
/// </summary>
|
||||
|
private FontStyle fontStyle = FontStyle.Bold; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The font size to render the text.
|
||||
|
/// </summary>
|
||||
|
private int fontSize = 48; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The position to start creating the text from.
|
||||
|
/// </summary>
|
||||
|
private Point position = Point.Empty; |
||||
|
#endregion
|
||||
|
|
||||
|
#region Properties
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets Text.
|
||||
|
/// </summary>
|
||||
|
public string Text { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets TextColor.
|
||||
|
/// </summary>
|
||||
|
public Color TextColor |
||||
|
{ |
||||
|
get { return this.textColor; } |
||||
|
set { this.textColor = value; } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the name of the font.
|
||||
|
/// </summary>
|
||||
|
public string Font { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the size of the font in pixels.
|
||||
|
/// </summary>
|
||||
|
public int FontSize |
||||
|
{ |
||||
|
get { return this.fontSize; } |
||||
|
set { this.fontSize = value; } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the FontStyle.
|
||||
|
/// </summary>
|
||||
|
public FontStyle Style |
||||
|
{ |
||||
|
get { return this.fontStyle; } |
||||
|
set { this.fontStyle = value; } |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the Opacity.
|
||||
|
/// </summary>
|
||||
|
public int Opacity |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
int alpha = (int)Math.Ceiling((this.opacity / 100d) * 255); |
||||
|
|
||||
|
return alpha < 255 ? alpha : 255; |
||||
|
} |
||||
|
|
||||
|
set |
||||
|
{ |
||||
|
this.opacity = value; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets Position.
|
||||
|
/// </summary>
|
||||
|
public Point Position |
||||
|
{ |
||||
|
get { return this.position; } |
||||
|
set { this.position = value; } |
||||
|
} |
||||
|
#endregion
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,332 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="Watermark.cs" company="James South">
|
||||
|
// TODO: Update copyright text.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace ImageProcessor.Processors |
||||
|
{ |
||||
|
#region Using
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Drawing; |
||||
|
using System.Text.RegularExpressions; |
||||
|
using ImageProcessor.Helpers.Extensions; |
||||
|
using ImageProcessor.Imaging; |
||||
|
#endregion
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates methods to change the alpha component of the image to effect its transparency.
|
||||
|
/// </summary>
|
||||
|
public class Watermark : IGraphicsProcessor |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for.
|
||||
|
/// http://stackoverflow.com/questions/11263949/optimize-a-variable-regex
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex QueryRegex = new Regex(@"watermark=(text-\w+\|position-\d+-\d+(\|color-([0-9a-fA-F]{3}){1,2}(\|size-\d+(\|style-(bold|italic|regular|strikeout|underline))?)?)?|\w+)", RegexOptions.Compiled); |
||||
|
|
||||
|
#region IGraphicsProcessor Members
|
||||
|
/// <summary>
|
||||
|
/// Gets the name.
|
||||
|
/// </summary>
|
||||
|
public string Name |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return "Watermark"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the description.
|
||||
|
/// </summary>
|
||||
|
public string Description |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return "Adds a watermark containing text to the image."; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the regular expression to search strings for.
|
||||
|
/// </summary>
|
||||
|
public Regex RegexPattern |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return QueryRegex; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets DynamicParameter.
|
||||
|
/// </summary>
|
||||
|
public dynamic DynamicParameter |
||||
|
{ |
||||
|
get; |
||||
|
set; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the order in which this processor is to be used in a chain.
|
||||
|
/// </summary>
|
||||
|
public int SortOrder |
||||
|
{ |
||||
|
get; |
||||
|
private set; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets any additional settings required by the processor.
|
||||
|
/// </summary>
|
||||
|
public Dictionary<string, string> Settings |
||||
|
{ |
||||
|
get; |
||||
|
set; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The position in the original string where the first character of the captured substring was found.
|
||||
|
/// </summary>
|
||||
|
/// <param name="queryString">
|
||||
|
/// The query string to search.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The zero-based starting position in the original string where the captured substring was found.
|
||||
|
/// </returns>
|
||||
|
public int MatchRegexIndex(string queryString) |
||||
|
{ |
||||
|
int index = 0; |
||||
|
|
||||
|
// Set the sort order to max to allow filtering.
|
||||
|
this.SortOrder = int.MaxValue; |
||||
|
|
||||
|
foreach (Match match in this.RegexPattern.Matches(queryString)) |
||||
|
{ |
||||
|
if (match.Success) |
||||
|
{ |
||||
|
if (index == 0) |
||||
|
{ |
||||
|
// Set the index on the first instance only.
|
||||
|
this.SortOrder = match.Index; |
||||
|
|
||||
|
TextLayer textLayer = new TextLayer(); |
||||
|
|
||||
|
// Split the string up.
|
||||
|
string[] firstPass = match.Value.Split('=')[1].Split('|'); |
||||
|
|
||||
|
switch (firstPass.Length) |
||||
|
{ |
||||
|
case 1: |
||||
|
textLayer.Text = firstPass[0]; |
||||
|
break; |
||||
|
case 2: |
||||
|
textLayer.Text = firstPass[0]; |
||||
|
textLayer.Position = this.GetPosition(firstPass[1]); |
||||
|
break; |
||||
|
|
||||
|
case 3: |
||||
|
textLayer.Text = firstPass[0]; |
||||
|
textLayer.Position = this.GetPosition(firstPass[1]); |
||||
|
textLayer.TextColor = this.GetColor(firstPass[2]); |
||||
|
break; |
||||
|
|
||||
|
case 4: |
||||
|
textLayer.Text = firstPass[0]; |
||||
|
textLayer.Position = this.GetPosition(firstPass[1]); |
||||
|
textLayer.TextColor = this.GetColor(firstPass[2]); |
||||
|
textLayer.FontSize = this.GetFontSize(firstPass[3]); |
||||
|
break; |
||||
|
|
||||
|
case 5: |
||||
|
textLayer.Text = firstPass[0]; |
||||
|
textLayer.Position = this.GetPosition(firstPass[1]); |
||||
|
textLayer.TextColor = this.GetColor(firstPass[2]); |
||||
|
textLayer.FontSize = this.GetFontSize(firstPass[3]); |
||||
|
textLayer.Style = this.GetFontStyle(firstPass[4]); |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
this.DynamicParameter = textLayer; |
||||
|
} |
||||
|
|
||||
|
index += 1; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return this.SortOrder; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Processes the image.
|
||||
|
/// </summary>
|
||||
|
/// <param name="factory">
|
||||
|
/// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
|
||||
|
/// the image to process.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
|
||||
|
/// </returns>
|
||||
|
public Image ProcessImage(ImageFactory factory) |
||||
|
{ |
||||
|
Bitmap newImage = null; |
||||
|
Image image = factory.Image; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
newImage = new Bitmap(image) { Tag = image.Tag }; |
||||
|
|
||||
|
TextLayer textLayer = this.DynamicParameter; |
||||
|
string text = textLayer.Text; |
||||
|
int opacity = textLayer.Opacity; |
||||
|
int fontSize = textLayer.FontSize; |
||||
|
FontStyle fontStyle = textLayer.Style; |
||||
|
|
||||
|
using (Graphics graphics = Graphics.FromImage(newImage)) |
||||
|
{ |
||||
|
using (Font font = this.GetFont(textLayer.Font, fontSize, fontStyle)) |
||||
|
{ |
||||
|
using (StringFormat drawFormat = new StringFormat()) |
||||
|
{ |
||||
|
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, textLayer.TextColor))) |
||||
|
{ |
||||
|
Point origin = textLayer.Position; |
||||
|
|
||||
|
// We need to ensure that there is a position set for the watermark
|
||||
|
if (origin == Point.Empty) |
||||
|
{ |
||||
|
// Work out the size of the text.
|
||||
|
SizeF textSize = graphics.MeasureString(text, font, new SizeF(image.Width, image.Height), drawFormat); |
||||
|
|
||||
|
int x = (int)(image.Width - textSize.Width) / 2; |
||||
|
int y = (int)(image.Height - textSize.Height) / 2; |
||||
|
origin = new Point(x, y); |
||||
|
} |
||||
|
|
||||
|
graphics.DrawString(text, font, brush, origin, drawFormat); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
image.Dispose(); |
||||
|
image = newImage; |
||||
|
} |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
if (newImage != null) |
||||
|
{ |
||||
|
newImage.Dispose(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return image; |
||||
|
} |
||||
|
#endregion
|
||||
|
|
||||
|
#region Private Methods
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.Font"/> for the given parameters
|
||||
|
/// </summary>
|
||||
|
/// <param name="font">
|
||||
|
/// The font.
|
||||
|
/// </param>
|
||||
|
/// <param name="fontSize">
|
||||
|
/// The font size.
|
||||
|
/// </param>
|
||||
|
/// <param name="fontStyle">
|
||||
|
/// The font style.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.Font"/>
|
||||
|
/// </returns>
|
||||
|
private Font GetFont(string font, int fontSize, FontStyle fontStyle) |
||||
|
{ |
||||
|
FontFamily fontFamily = string.IsNullOrEmpty(font) ? FontFamily.GenericSansSerif : new FontFamily(font); |
||||
|
|
||||
|
return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.Point"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.Point"/>
|
||||
|
/// </returns>
|
||||
|
private Point GetPosition(string input) |
||||
|
{ |
||||
|
int[] position = input.ToIntegerArray(); |
||||
|
int x = position[0]; |
||||
|
int y = position[1]; |
||||
|
|
||||
|
return new Point(x, y); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.Color"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.Color"/>
|
||||
|
/// </returns>
|
||||
|
private Color GetColor(string input) |
||||
|
{ |
||||
|
// split on color-hex
|
||||
|
return ColorTranslator.FromHtml("#" + input.Split('-')[1]); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Int32"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Int32"/>
|
||||
|
/// </returns>
|
||||
|
private int GetFontSize(string input) |
||||
|
{ |
||||
|
// split on size-value
|
||||
|
return int.Parse(input.Split('-')[1]); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.FontStyle"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The string containing the respective fontstyle.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.FontStyle"/>
|
||||
|
/// </returns>
|
||||
|
private FontStyle GetFontStyle(string input) |
||||
|
{ |
||||
|
FontStyle fontStyle = FontStyle.Bold; |
||||
|
|
||||
|
switch (input.ToLowerInvariant()) |
||||
|
{ |
||||
|
case "italic": |
||||
|
fontStyle = FontStyle.Italic; |
||||
|
break; |
||||
|
case "regular": |
||||
|
fontStyle = FontStyle.Regular; |
||||
|
break; |
||||
|
case "strikeout": |
||||
|
fontStyle = FontStyle.Strikeout; |
||||
|
break; |
||||
|
case "underline": |
||||
|
fontStyle = FontStyle.Underline; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
return fontStyle; |
||||
|
} |
||||
|
#endregion
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,444 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="Watermark.cs" company="James South">
|
||||
|
// TODO: Update copyright text.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace ImageProcessor.Processors |
||||
|
{ |
||||
|
#region Using
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Drawing; |
||||
|
using System.Drawing.Text; |
||||
|
using System.Text.RegularExpressions; |
||||
|
using ImageProcessor.Helpers.Extensions; |
||||
|
using ImageProcessor.Imaging; |
||||
|
#endregion
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Encapsulates methods to change the alpha component of the image to effect its transparency.
|
||||
|
/// </summary>
|
||||
|
public class Watermark : IGraphicsProcessor |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex QueryRegex = new Regex(@"watermark=[^&]*", RegexOptions.Compiled); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for the text attribute.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex TextRegex = new Regex(@"text-[^/:?#\[\]@!$&'()*%\|,;=]+", RegexOptions.Compiled); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for the position attribute.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex PositionRegex = new Regex(@"position-\d+-\d+", RegexOptions.Compiled); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for the color attribute.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex ColorRegex = new Regex(@"color-([0-9a-fA-F]{3}){1,2}", RegexOptions.Compiled); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for the fontsize attribute.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex FontSizeRegex = new Regex(@"size-\d{1,3}", RegexOptions.Compiled); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for the fontstyle attribute.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex FontStyleRegex = new Regex(@"style-(bold|italic|regular|strikeout|underline)", RegexOptions.Compiled); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for the font family attribute.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex FontFamilyRegex = new Regex(@"font-[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The regular expression to search strings for the opacity attribute.
|
||||
|
/// </summary>
|
||||
|
private static readonly Regex OpacityRegex = new Regex(@"opacity-\d{1,2}(?!\d)|opacity-100", RegexOptions.Compiled); |
||||
|
|
||||
|
#region IGraphicsProcessor Members
|
||||
|
/// <summary>
|
||||
|
/// Gets the name.
|
||||
|
/// </summary>
|
||||
|
public string Name |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return "Watermark"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the description.
|
||||
|
/// </summary>
|
||||
|
public string Description |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return "Adds a watermark containing text to the image."; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the regular expression to search strings for.
|
||||
|
/// </summary>
|
||||
|
public Regex RegexPattern |
||||
|
{ |
||||
|
get |
||||
|
{ |
||||
|
return QueryRegex; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets DynamicParameter.
|
||||
|
/// </summary>
|
||||
|
public dynamic DynamicParameter |
||||
|
{ |
||||
|
get; |
||||
|
set; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the order in which this processor is to be used in a chain.
|
||||
|
/// </summary>
|
||||
|
public int SortOrder |
||||
|
{ |
||||
|
get; |
||||
|
private set; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets any additional settings required by the processor.
|
||||
|
/// </summary>
|
||||
|
public Dictionary<string, string> Settings |
||||
|
{ |
||||
|
get; |
||||
|
set; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// The position in the original string where the first character of the captured substring was found.
|
||||
|
/// </summary>
|
||||
|
/// <param name="queryString">
|
||||
|
/// The query string to search.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The zero-based starting position in the original string where the captured substring was found.
|
||||
|
/// </returns>
|
||||
|
public int MatchRegexIndex(string queryString) |
||||
|
{ |
||||
|
int index = 0; |
||||
|
|
||||
|
// Set the sort order to max to allow filtering.
|
||||
|
this.SortOrder = int.MaxValue; |
||||
|
|
||||
|
foreach (Match match in this.RegexPattern.Matches(queryString)) |
||||
|
{ |
||||
|
if (match.Success) |
||||
|
{ |
||||
|
if (index == 0) |
||||
|
{ |
||||
|
// Set the index on the first instance only.
|
||||
|
this.SortOrder = match.Index; |
||||
|
|
||||
|
TextLayer textLayer = new TextLayer(); |
||||
|
|
||||
|
string toParse = match.Value; |
||||
|
|
||||
|
textLayer.Text = this.ParseText(toParse); |
||||
|
textLayer.Position = this.ParsePosition(toParse); |
||||
|
textLayer.TextColor = this.ParseColor(toParse); |
||||
|
textLayer.FontSize = this.ParseFontSize(toParse); |
||||
|
textLayer.Font = this.ParseFontFamily(toParse); |
||||
|
textLayer.Style = this.ParseFontStyle(toParse); |
||||
|
textLayer.Opacity = this.ParseOpacity(toParse); |
||||
|
|
||||
|
this.DynamicParameter = textLayer; |
||||
|
} |
||||
|
|
||||
|
index += 1; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return this.SortOrder; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Processes the image.
|
||||
|
/// </summary>
|
||||
|
/// <param name="factory">
|
||||
|
/// The the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class containing
|
||||
|
/// the image to process.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The processed image from the current instance of the <see cref="T:ImageProcessor.ImageFactory"/> class.
|
||||
|
/// </returns>
|
||||
|
public Image ProcessImage(ImageFactory factory) |
||||
|
{ |
||||
|
Bitmap newImage = null; |
||||
|
Image image = factory.Image; |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
// Dont use an object initializer here.
|
||||
|
newImage = new Bitmap(image); |
||||
|
newImage.Tag = image.Tag; |
||||
|
|
||||
|
TextLayer textLayer = this.DynamicParameter; |
||||
|
string text = textLayer.Text; |
||||
|
int opacity = textLayer.Opacity; |
||||
|
int fontSize = textLayer.FontSize; |
||||
|
FontStyle fontStyle = textLayer.Style; |
||||
|
|
||||
|
using (Graphics graphics = Graphics.FromImage(newImage)) |
||||
|
{ |
||||
|
using (Font font = this.GetFont(textLayer.Font, fontSize, fontStyle)) |
||||
|
{ |
||||
|
using (StringFormat drawFormat = new StringFormat()) |
||||
|
{ |
||||
|
using (Brush brush = new SolidBrush(Color.FromArgb(opacity, textLayer.TextColor))) |
||||
|
{ |
||||
|
Point origin = textLayer.Position; |
||||
|
|
||||
|
// We need to ensure that there is a position set for the watermark
|
||||
|
if (origin == Point.Empty) |
||||
|
{ |
||||
|
// Work out the size of the text.
|
||||
|
SizeF textSize = graphics.MeasureString(text, font, new SizeF(image.Width, image.Height), drawFormat); |
||||
|
|
||||
|
int x = (int)(image.Width - textSize.Width) / 2; |
||||
|
int y = (int)(image.Height - textSize.Height) / 2; |
||||
|
origin = new Point(x, y); |
||||
|
} |
||||
|
|
||||
|
// Set the hinting and draw the text.
|
||||
|
graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; |
||||
|
graphics.DrawString(text, font, brush, origin, drawFormat); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
image.Dispose(); |
||||
|
image = newImage; |
||||
|
} |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
if (newImage != null) |
||||
|
{ |
||||
|
newImage.Dispose(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return image; |
||||
|
} |
||||
|
#endregion
|
||||
|
|
||||
|
#region Private Methods
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.Font"/> for the given parameters
|
||||
|
/// </summary>
|
||||
|
/// <param name="font">
|
||||
|
/// The font.
|
||||
|
/// </param>
|
||||
|
/// <param name="fontSize">
|
||||
|
/// The font size.
|
||||
|
/// </param>
|
||||
|
/// <param name="fontStyle">
|
||||
|
/// The font style.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.Font"/>
|
||||
|
/// </returns>
|
||||
|
private Font GetFont(string font, int fontSize, FontStyle fontStyle) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
using (FontFamily fontFamily = new FontFamily(font)) |
||||
|
{ |
||||
|
return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel); |
||||
|
} |
||||
|
} |
||||
|
catch |
||||
|
{ |
||||
|
using (FontFamily fontFamily = FontFamily.GenericSansSerif) |
||||
|
{ |
||||
|
return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.String"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.String"/> for the given string.
|
||||
|
/// </returns>
|
||||
|
private string ParseText(string input) |
||||
|
{ |
||||
|
foreach (Match match in TextRegex.Matches(input)) |
||||
|
{ |
||||
|
// split on text-
|
||||
|
return match.Value.Split('-')[1].Replace("+", " "); |
||||
|
} |
||||
|
|
||||
|
return string.Empty; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.Point"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.Point"/>
|
||||
|
/// </returns>
|
||||
|
private Point ParsePosition(string input) |
||||
|
{ |
||||
|
foreach (Match match in PositionRegex.Matches(input)) |
||||
|
{ |
||||
|
int[] position = match.Value.ToIntegerArray(); |
||||
|
|
||||
|
if (position != null) |
||||
|
{ |
||||
|
int x = position[0]; |
||||
|
int y = position[1]; |
||||
|
|
||||
|
return new Point(x, y); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return Point.Empty; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.Color"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.Color"/>
|
||||
|
/// </returns>
|
||||
|
private Color ParseColor(string input) |
||||
|
{ |
||||
|
foreach (Match match in ColorRegex.Matches(input)) |
||||
|
{ |
||||
|
// split on color-hex
|
||||
|
return ColorTranslator.FromHtml("#" + match.Value.Split('-')[1]); |
||||
|
} |
||||
|
|
||||
|
return Color.Black; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Int32"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Int32"/>
|
||||
|
/// </returns>
|
||||
|
private int ParseFontSize(string input) |
||||
|
{ |
||||
|
foreach (Match match in FontSizeRegex.Matches(input)) |
||||
|
{ |
||||
|
// split on size-value
|
||||
|
return int.Parse(match.Value.Split('-')[1]); |
||||
|
} |
||||
|
|
||||
|
// Matches the default number in TextLayer.
|
||||
|
return 48; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Drawing.FontStyle"/> for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The string containing the respective fontstyle.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Drawing.FontStyle"/>
|
||||
|
/// </returns>
|
||||
|
private FontStyle ParseFontStyle(string input) |
||||
|
{ |
||||
|
FontStyle fontStyle = FontStyle.Bold; |
||||
|
|
||||
|
foreach (Match match in FontStyleRegex.Matches(input)) |
||||
|
{ |
||||
|
// split on style-
|
||||
|
switch (match.Value.Split('-')[1]) |
||||
|
{ |
||||
|
case "italic": |
||||
|
fontStyle = FontStyle.Italic; |
||||
|
break; |
||||
|
case "regular": |
||||
|
fontStyle = FontStyle.Regular; |
||||
|
break; |
||||
|
case "strikeout": |
||||
|
fontStyle = FontStyle.Strikeout; |
||||
|
break; |
||||
|
case "underline": |
||||
|
fontStyle = FontStyle.Underline; |
||||
|
break; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return fontStyle; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.String"/> containing the font family for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.String"/> containing the font family for the given string.
|
||||
|
/// </returns>
|
||||
|
private string ParseFontFamily(string input) |
||||
|
{ |
||||
|
foreach (Match match in FontFamilyRegex.Matches(input)) |
||||
|
{ |
||||
|
// split on font-
|
||||
|
string font = match.Value.Split('-')[1].Replace("+", " "); |
||||
|
|
||||
|
return font; |
||||
|
} |
||||
|
|
||||
|
return string.Empty; |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Returns the correct <see cref="T:System.Int32"/> containing the opacity for the given string.
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">
|
||||
|
/// The input string containing the value to parse.
|
||||
|
/// </param>
|
||||
|
/// <returns>
|
||||
|
/// The correct <see cref="T:System.Int32"/> containing the font family for the given string.
|
||||
|
/// </returns>
|
||||
|
private int ParseOpacity(string input) |
||||
|
{ |
||||
|
foreach (Match match in OpacityRegex.Matches(input)) |
||||
|
{ |
||||
|
// split on opacity-
|
||||
|
return int.Parse(match.Value.Split('-')[1]); |
||||
|
} |
||||
|
|
||||
|
// full opacity - matches the Textlayer default.
|
||||
|
return 100; |
||||
|
} |
||||
|
#endregion
|
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue