mirror of
https://github.com/ShareX/ShareX.git
synced 2024-09-28 15:31:30 +12:00
Workaround for "System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
This commit is contained in:
parent
91dd6eb296
commit
0ded947b1e
6 changed files with 142 additions and 53 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -106,3 +106,6 @@ Generated_Code #added for RIA/Silverlight projects
|
|||
_UpgradeReport_Files/
|
||||
Backup*/
|
||||
UpgradeLog*.XML
|
||||
|
||||
# Additional
|
||||
*.exe
|
62
HelpersLib/FontSafe.cs
Normal file
62
HelpersLib/FontSafe.cs
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -86,6 +86,7 @@
|
|||
<Compile Include="Colors\RGBA.cs" />
|
||||
<Compile Include="ConvolutionMatrix.cs" />
|
||||
<Compile Include="ConvolutionMatrixManager.cs" />
|
||||
<Compile Include="FontSafe.cs" />
|
||||
<Compile Include="UITypeEditors\EnumDescriptionConverter.cs" />
|
||||
<Compile Include="UITypeEditors\MyColorConverter.cs" />
|
||||
<Compile Include="UITypeEditors\MyColorEditor.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
1
ImageEffectsLib/ImageEffectsForm.Designer.cs
generated
1
ImageEffectsLib/ImageEffectsForm.Designer.cs
generated
|
@ -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
|
||||
|
|
|
@ -57,7 +57,6 @@ public ImageEffectsForm(Image img, List<ImageEffect> effects = null)
|
|||
|
||||
public void EditorMode()
|
||||
{
|
||||
btnRefresh.Visible = true;
|
||||
btnLoadImage.Visible = true;
|
||||
btnSaveImage.Visible = true;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue