Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows GDI+
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?
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 5-Apr-13 23:32pm
Edited 7-Apr-13 0:38am
v4
Comments
Neelendu Kumar at 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
good
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).
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 390
1 Jochen Arndt 150
2 Richard MacCutchan 135
3 DamithSL 95
4 Garth J Lancaster 90
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,260


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2014
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