【问题标题】:OutOfMemoryException when Textbox is null当文本框为空时出现 OutOfMemoryException
【发布时间】:2023-03-28 09:09:01
【问题描述】:

每次我在我的文本框中输入,文本完美地绘制在图片框中,但每当我删除所有字母或先输入[空格]时,图片框变成这样:

TextBox 文本更改事件

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;
        }
    }

【问题讨论】:

  • 那么可以重现问题所需的最少代码量是多少,可以用它来测试。如果将其粘贴到 Visual Studio 中,它将无法编译
  • @TheGeneral 更新了问题中的代码。希望对你有帮助
  • SystemOutOfMemory 异常显然意味着你没有足够的内存来运行这个方法,可能你有一些内存泄漏,或者你的电脑内存不足
  • 我的笔记本电脑使用 8 GB 内存。如何检测这些内存泄漏? @MarkiianBenovskyi
  • .Net 默认只能使用 2gb 的内存,请在任务管理器中查看您的应用使用了多少内存。 2题就是2题,请将题一分为二,调整标题,没有人可以搜索'2题修改文字'。

标签: c# winforms text


【解决方案1】:

使用此条件修复它:

if (text == "" || text == " ")
                path.Dispose();

【讨论】:

  • 为什么有条件?您应该始终处置 IDisposable。
  • 当我绘制文本时,它调用了两次该方法。我不知道为什么,但我还不能处理它。
  • 那么你的资源管理搞砸了,你仍然有泄漏......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 2012-10-05
  • 2020-02-10
相关资源
最近更新 更多