Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i draw ellipse at runtime using following code.in 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?
C#
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

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

                this.Invalidate();
            }
        }

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            g = this.CreateGraphics();
            gp = new GraphicsPath();
            Pen pen = new Pen(Color.Red);
            gp.AddEllipse(r);
            gp.Widen(pen);
            pen.DashStyle = DashStyle.Dash;
            if (IsDrowing)
            {
                g.DrawPath(pen, gp);
            }


            Rectangle rect=new Rectangle(100,200,100,90);
            gp1 = new GraphicsPath();
            gp1.AddRectangle(rect);
            gp1.Widen(Pens.Red);
            e.Graphics.DrawPath(Pens.Red,gp1);

            Rectangle rect1 = new Rectangle(300, 200, 100, 90);
            gp2 = new GraphicsPath();
            gp2.AddEllipse(rect1);
            gp2.Widen(Pens.Red);
            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();
            gp3.AddCurve(points.ToArray());

            gp3.Widen(Pens.Blue);
            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;
            this.Refresh();
            isDown = false;
            g=this.CreateGraphics();
            if (contained)
            {
                g.DrawPath(Pens.Black, gp1);
            }
            if (containedE)
            {
                g.DrawPath(Pens.Black, gp2);
            }
            if (containedC)
            {
                g.DrawPath(Pens.Black, gp3);
            }
        }
    }
Posted
Updated 6-Apr-13 23:38pm
v4
Comments
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

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).
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900