代码之家  ›  专栏  ›  技术社区  ›  TerribleDog

Textbox为null时OutOfMemoryException

  •  1
  • TerribleDog  · 技术社区  · 7 年前

    每次我在文本框中键入文字时,都会在picurebox中完美地绘制文字,但每当我删除所有字母或先键入[space]时,picturebox就会变成这样:

    enter image description here

    文本框文本更改事件

    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、\u fontStyle.fontfamine、Convert.ToInt32(\u fontStyle.Style)、r.高度、r.位置、字符串格式);

    编辑:

    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;
            }
        }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   TerribleDog    7 年前

    使用以下条件修复:

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