C# 文本框为空时OutOfMemoryException
每次我在文本框中输入文本时,文本都会完美地绘制在picurebox中,但每当我先擦除所有字母或键入[space]时,picturebox就会变成这样: 文本框文本更改事件C# 文本框为空时OutOfMemoryException,c#,winforms,text,C#,Winforms,Text,每次我在文本框中输入文本时,文本都会完美地绘制在picurebox中,但每当我先擦除所有字母或键入[space]时,picturebox就会变成这样: 文本框文本更改事件 private void tbox_Text_TextChanged(object sender, EventArgs e) { _LayerType = (LayerClass.Type)System.Enum.Parse(typeof(LayerClass.Type), "Text");
private void tbox_Text_TextChanged(object sender, EventArgs e)
{
_LayerType = (LayerClass.Type)System.Enum.Parse(typeof(LayerClass.Type), "Text");
pictureBox_Canvass.Invalidate();
text = tbox_Text.Text;
UpdateFont();
textRect = txt.MeasureCharacters(fontFamily, text);
}
测量字符方法
public RectangleF MeasureCharacters(Font f, string text)
{
RectangleF r = new RectangleF();
GraphicsPath path = new GraphicsPath();
path.AddString(text, f.FontFamily, (int)f.Style, f.Size, new PointF(250 - (r.Width / 2), 250 - (r.Height / 2)), StringFormat.GenericDefault);
var bounds = path.GetBounds();
r = new RectangleF(bounds.Left, bounds.Top, bounds.Width, bounds.Height);
return r;
}
文本绘图
public LayerClass DrawString(LayerClass.Type _text, string text, RectangleF rect, Font _fontStyle, Brush brush, float angle, PaintEventArgs e)
{
using (StringFormat string_format = new StringFormat())
{
SizeF stringSize = e.Graphics.MeasureString(text, _fontStyle);
rect.Location = new PointF(Shape.center.X - (rect.Width / 2), Shape.center.Y - (rect.Height / 2));
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
//e.Graphics.DrawRectangle(Pens.Red, Rectangle.Round(rect));
RectangleF r = new RectangleF(rect.Location, rect.Size);
GraphicsPath path = new GraphicsPath();
//Exception thrown here
path.AddString(text, _fontStyle.FontFamily, Convert.ToInt32(_fontStyle.Style), r.Height, r.Location, string_format);
RectangleF text_rectf = path.GetBounds();
PointF[] target_pts = {
new PointF(r.Left, r.Top),
new PointF(r.Right, r.Top),
new PointF(r.Left, r.Bottom)};
Matrix m = new Matrix(text_rectf, target_pts);
Matrix flip = new Matrix();
flip.Translate(-stringSize.Width, 1);
m.RotateAt(angle, new PointF(Shape.center.X - (r.Width/4.5f), Shape.center.Y));
path.Transform(m);
if (flipped)
path.Transform(flip);
if (!isOutlined)
e.Graphics.FillPath(Brushes.Red, path);
else
e.Graphics.DrawPath(Pens.Red, path);
}
this._Text = text;
this._TextRect = rect;
this.brush = brush;
this._Angle = angle;
return new LayerClass(_text, this, text, rect);
}
错误:
System.Drawing.dll中出现“System.OutOfMemoryException”类型的首次意外异常
例外情况来自
AddString(文本,_fontStyle.fontfaliam,Convert.ToInt32(_fontStyle.Style),r.高度,r.位置,字符串格式)
编辑:
这在我的onPaint事件中被调用:
public void Source(PaintEventArgs e)
{
switch (_LayerType)
{
case LayerClass.Type.Rectangle:
shape.DrawRectangle(LayerClass.Type.Rectangle, rectangleColor, strokeRect, rectangleWidth, rectangleHeight, e.Graphics, rectRadius);
break;
case LayerClass.Type.Square:
shape.DrawSquare(LayerClass.Type.Square, squareColor, strokeSquare, squareWidth, squareHeight, e.Graphics, squareRadius);
break;
case LayerClass.Type.Circle:
shape.DrawCircle(LayerClass.Type.Circle, circleColor, strokeCircle, circleWidth, circleHeight, e.Graphics);
break;
case LayerClass.Type.Ellipse:
shape.DrawEllipse(LayerClass.Type.Ellipse, ellipseColor, strokeEllipse, ellipseWidth, ellipseHeight, e.Graphics);
break;
case LayerClass.Type.Triangle:
shape.DrawTriangle(LayerClass.Type.Triangle, triangleColor, strokeTriangle, triangleWidth, e.Graphics, triangleRadius);
break;
case LayerClass.Type.Image:
img.ImageDrawing(LayerClass.Type.Image, ImageBitmap, imgRect, path, rotationAngle, e, newLoc, flipped);
break;
case LayerClass.Type.Text:
txt.DrawString(LayerClass.Type.Text, text, textRect, fontFamily, brush, textAngle, e); //CALLS THE TEXT DRAWING
break;
}
}
使用以下条件修复它:
if (text == "" || text == " ")
path.Dispose();
那么,能够重现问题所需的最小代码量是多少,可以用什么进行测试呢。如果将此粘贴到visual studio中,则不会compile@TheGeneral更新了问题中的代码。希望它能帮助SystememootfMemory异常显然意味着您没有足够的RAM来运行此方法,可能是内存泄漏,或者您的电脑内存不足。我的笔记本电脑使用8 gb的RAM。如何检测这些内存泄漏@默认情况下,MarkiAnbeNovskyi.Net只能使用2gb内存,请检查任务管理器您的应用程序使用了多少内存。2个问题就是2个问题,请把你的问题一分为二并调整标题,没有人可以搜索“修改文本的2个问题”。为什么有条件?您应该始终处理IDisposable。当我绘制文本时,它会调用该方法两次。我不知道为什么,但我还不能处理它。然后你的资源管理混乱,你仍然有漏洞。。。