1

Everytime I type in my textbox, the text perfectly draws in the picurebox, but whenever I erase all the letters or type a [space] first, the picturebox becomes like this:

enter image description here

TextBox Text Change Event

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

Measure Characters Method

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

Text Drawing

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

Error:

A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll

The exception comes from the line in the

path.AddString(text, _fontStyle.FontFamily, Convert.ToInt32(_fontStyle.Style), r.Height, r.Location, string_format);

EDIT:

This is called on my onPaint Event:

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;
        }
    }
TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • So whats the minimal amount of code needed that can reproduce the problem, that can be tested with. If pasted this into visual studio it wouldn't compile – TheGeneral Nov 06 '18 at 08:31
  • @TheGeneral updated the code in the question. Hope it helps – TerribleDog Nov 06 '18 at 08:36
  • 1
    SystemOutOfMemory exception obviously means that you have not enough RAM to run this method, probably you have some memory leak, or your PC is running low on memory – Markiian Benovskyi Nov 06 '18 at 08:41
  • I use 8 gb ram for my laptop. How do I detect those memory leaks? @MarkiianBenovskyi – TerribleDog Nov 06 '18 at 08:43
  • 1
    .Net can only use 2gb of ram by default, check the task manager how much memory is used by your app. And 2 problems are 2 questions, please divide your question into two and adjust your title, nobody can search for '2 Problems on Modifying text'. – Rabban Nov 06 '18 at 08:49
  • @Rabban updated the question. The app only uses 16mb of RAM – TerribleDog Nov 06 '18 at 08:52
  • A GraphicsPath is `IDisposable`. You could have other memory/resource leaks, hard to tell. – bommelding Nov 06 '18 at 08:54
  • Better post the Paint eventhandler that calls this. – bommelding Nov 06 '18 at 08:55
  • @bommelding Done – TerribleDog Nov 06 '18 at 08:59
  • OutOfMemoryException isn't related to the total amout of physical memory. https://learn.microsoft.com/en-us/dotnet/api/system.outofmemoryexception?view=netframework-4.7.2 maybe this could help – Jimbot Nov 06 '18 at 09:14
  • System.Drawing classes can also [raise this exception](https://stackoverflow.com/questions/2610416/is-there-a-reason-image-fromfile-throws-an-outofmemoryexception-for-an-invalid-i) when you have plenty of RAM and address space left. The GraphicsPath and Matrix classes have a Dispose() method, you are not using it. You tend to get away with it if the garbage collector runs often enough. Until it doesn't. Task Manager can be helpful to see this going wrong, add the GDI Objects and USER Objects columns. – Hans Passant Nov 06 '18 at 10:14
  • GDI+ sometimes throw OutOfMemory exception because it can't interpret the error code from native GDI. This could happen when the parameters passing to the `AddString` method are not valid. Could you please check every parameter on the line that you got the exception? – xtu Nov 06 '18 at 10:50

1 Answers1

1

Fixed it using this condition:

if (text == "" || text == " ")
                path.Dispose();
TerribleDog
  • 1,237
  • 1
  • 8
  • 31