diff --git a/.gitignore b/.gitignore index bdc3535f7..56f01ba3b 100644 --- a/.gitignore +++ b/.gitignore @@ -106,3 +106,6 @@ Generated_Code #added for RIA/Silverlight projects _UpgradeReport_Files/ Backup*/ UpgradeLog*.XML + +# Additional +*.exe \ No newline at end of file diff --git a/HelpersLib/FontSafe.cs b/HelpersLib/FontSafe.cs new file mode 100644 index 000000000..3ce264948 --- /dev/null +++ b/HelpersLib/FontSafe.cs @@ -0,0 +1,62 @@ +#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 . +*/ + +#endregion License Information (GPL v3) + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; + +namespace HelpersLib +{ + public class FontSafe + { + public string Font { get; set; } + + public FontSafe() + { + } + + public FontSafe(Font font) + { + SetFont(font); + } + + public void SetFont(Font font) + { + Font = new FontConverter().ConvertToInvariantString(font); + } + + public Font GetFont() + { + if (!string.IsNullOrEmpty(Font)) + { + return new FontConverter().ConvertFromInvariantString(Font) as Font; + } + + return null; + } + } +} \ No newline at end of file diff --git a/HelpersLib/HelpersLib.csproj b/HelpersLib/HelpersLib.csproj index 8620bd251..f256144e4 100644 --- a/HelpersLib/HelpersLib.csproj +++ b/HelpersLib/HelpersLib.csproj @@ -86,6 +86,7 @@ + diff --git a/ImageEffectsLib/Drawings/DrawText.cs b/ImageEffectsLib/Drawings/DrawText.cs index 83609d2f2..250fab4fe 100644 --- a/ImageEffectsLib/Drawings/DrawText.cs +++ b/ImageEffectsLib/Drawings/DrawText.cs @@ -49,8 +49,24 @@ internal class DrawText : ImageEffect [DefaultValue("%h:%mi")] public string Text { get; set; } + 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." [DefaultValue(typeof(Font), "Arial, 8pt")] - public Font TextFont { get; set; } + public Font TextFont + { + get + { + return textFontSafe.GetFont(); + } + set + { + using (value) + { + textFontSafe.SetFont(value); + } + } + } [DefaultValue(typeof(Color), "White"), Editor(typeof(MyColorEditor), typeof(UITypeEditor)), TypeConverter(typeof(MyColorConverter))] public Color TextColor { get; set; } @@ -86,74 +102,83 @@ public DrawText() public override Image Apply(Image img) { - if (string.IsNullOrEmpty(Text) || TextFont == null || TextFont.Size < 1) + if (string.IsNullOrEmpty(Text)) { return 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))) + using (Font textFont = TextFont) { - 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) + if (textFont == null || textFont.Size < 1) { - using (GraphicsPath gPath = new GraphicsPath()) + return 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) { - gPath.AddRoundedRectangle(labelRectangle, CornerRadius); - - Brush backgroundBrush = null; - - try + using (GraphicsPath gPath = new GraphicsPath()) { - if (UseGradient) + gPath.AddRoundedRectangle(labelRectangle, CornerRadius); + + Brush backgroundBrush = null; + + try { - backgroundBrush = new LinearGradientBrush(labelRectangle, BackgroundColor, BackgroundColor2, GradientType); + if (UseGradient) + { + backgroundBrush = new LinearGradientBrush(labelRectangle, BackgroundColor, BackgroundColor2, GradientType); + } + else + { + backgroundBrush = new SolidBrush(BackgroundColor); + } + + g.FillPath(backgroundBrush, gPath); } - else + finally { - backgroundBrush = new SolidBrush(BackgroundColor); + if (backgroundBrush != null) backgroundBrush.Dispose(); } - g.FillPath(backgroundBrush, gPath); - } - finally - { - if (backgroundBrush != null) backgroundBrush.Dispose(); - } - - using (Pen borderPen = new Pen(BorderColor)) - { - g.DrawPath(borderPen, gPath); + 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 (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(); + 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); + Point labelPosition = Helpers.GetPosition(Position, Offset, img.Size, labelSize); + gImg.DrawImage(bmp, labelPosition.X, labelPosition.Y, bmp.Width, bmp.Height); + } } } diff --git a/ImageEffectsLib/ImageEffectsForm.Designer.cs b/ImageEffectsLib/ImageEffectsForm.Designer.cs index 24d8c2265..775934efe 100644 --- a/ImageEffectsLib/ImageEffectsForm.Designer.cs +++ b/ImageEffectsLib/ImageEffectsForm.Designer.cs @@ -197,7 +197,6 @@ private void InitializeComponent() this.btnRefresh.TabIndex = 14; this.btnRefresh.Text = "Refresh"; this.btnRefresh.UseVisualStyleBackColor = true; - this.btnRefresh.Visible = false; this.btnRefresh.Click += new System.EventHandler(this.btnRefresh_Click); // // btnLoadImage diff --git a/ImageEffectsLib/ImageEffectsForm.cs b/ImageEffectsLib/ImageEffectsForm.cs index 5fbae10c0..d9074de11 100644 --- a/ImageEffectsLib/ImageEffectsForm.cs +++ b/ImageEffectsLib/ImageEffectsForm.cs @@ -57,7 +57,6 @@ public ImageEffectsForm(Image img, List effects = null) public void EditorMode() { - btnRefresh.Visible = true; btnLoadImage.Visible = true; btnSaveImage.Visible = true; }