每次我在文本框中键入内容时,文本都会完美地绘制在picurebox中,但是每当我擦除所有字母或先键入[空格]时,图片框就会变成这样:
文本框文本更改事件
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'的第一次机会异常
该异常来自
中的行path.AddString(text,_fontStyle.FontFamily,Convert.ToInt32(_fontStyle.Style),r.Height,r.Location,string_format);
编辑:
这是在我的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;
}
}
答案 0 :(得分:1)
使用以下条件修复了该问题:
if (text == "" || text == " ")
path.Dispose();