Click here to Skip to main content
13,050,904 members (62,420 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i draw ellipse at runtime using following that code i used graphics path for drawing(actually this is project requirement )and used widen method for graphics path.
but it gives runtime exception "out of memory".can i use this method in the case of ellipse?
while using widen method in the case of drawing rectangle at runtime, its working properly.

please solve this problem and give me some suggestion?
public partial class Form2 : Form
        public Form2()
        Rectangle r;
        bool isDown = false;
        int initialX;
        int initialY;
        bool IsDrowing =true;
        GraphicsPath gp1;
        GraphicsPath gp2;
        GraphicsPath gp3;
        GraphicsPath gp;
        Graphics g;
        bool contained;
        bool containedE;
        bool containedC;
        private void Form2_MouseDown(object sender, MouseEventArgs e)
            isDown = true;
            IsDrowing = true;
            initialX = e.X;
            initialY = e.Y;
        private void Form2_MouseMove(object sender, MouseEventArgs e)
            //IsDrowing = true;
            if (isDown == true)
                int width = e.X - initialX, height = e.Y - initialY;
                r = new Rectangle(Math.Min(e.X, initialX),
                               Math.Min(e.Y, initialY),
                               Math.Abs(e.X - initialX),
                               Math.Abs(e.Y - initialY));
        private void Form2_Paint(object sender, PaintEventArgs e)
            g = this.CreateGraphics();
            gp = new GraphicsPath();
            Pen pen = new Pen(Color.Red);
            pen.DashStyle = DashStyle.Dash;
            if (IsDrowing)
                g.DrawPath(pen, gp);

            Rectangle rect=new Rectangle(100,200,100,90);
            gp1 = new GraphicsPath();
            Rectangle rect1 = new Rectangle(300, 200, 100, 90);
            gp2 = new GraphicsPath();
            e.Graphics.DrawPath(Pens.Red, gp2);
            List<point> points = new List<point>();
            points.Add(new Point(10, 10));
            points.Add(new Point(100, 30));
            points.Add(new Point(200, 80));
            points.Add(new Point(500, 15));
            gp3 = new GraphicsPath();
            e.Graphics.DrawPath(Pens.Blue, gp3);

            Region r1 = new Region(gp1);
            contained = r1.IsVisible(r);
            Region r2 = new System.Drawing.Region(gp2);
            containedE = r2.IsVisible(r);
            Region r3 = new Region(gp3);
            containedC = r3.IsVisible(r);
        private void Form2_MouseUp(object sender, MouseEventArgs e)
            IsDrowing = false;
            isDown = false;
            if (contained)
                g.DrawPath(Pens.Black, gp1);
            if (containedE)
                g.DrawPath(Pens.Black, gp2);
            if (containedC)
                g.DrawPath(Pens.Black, gp3);
Posted 5-Apr-13 22:32pm
Updated 6-Apr-13 23:38pm
Neelendu Kumar 6-Apr-13 4:34am
this is project for selecting the drawn shape when we drag any ellipse over to it.
please help me

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Without actually trying it (and I'm not about to do that) the most obvious cause would look like you being lazy: you are creating graphics objects such as Graphic Contexts and Pens like crazy in your Paint event without even trying to Dispose any of them. Since some of these are scarce resources which will run out well before the actual memory does the Garbage Collector does not get kicked in and get rid of them for you. This can cause an "out of memory" error long before the RAM memory is exhausted. Try making sure that whenever you create an object that implements IDisposable that you call the Dispose method when you have finished with it (either explicitly via the Dispose method, or implicitly by surrounding it with a using block).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100