在运行时绘制椭圆并使用widen metod。 [英] draw ellipse at runtime and use widen metod.

查看:62
本文介绍了在运行时绘制椭圆并使用widen metod。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行时使用以下代码绘制椭圆。在该代码中我使用图形路径进行绘图(实际上这是项目要求)并使用了扩展的图形路径方法。

但是它给出了运行时异常内存不足。我可以在椭圆的情况下使用这种方法吗?

在运行时绘制矩形的情况下使用widen方法时,它的工作正常。



请解决这个问题并给我一些建议吗?

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

推荐答案

没有真正尝试过(我不是要做到这一点)最明显的原因看起来就像你在懒惰:你在Paint事件中创建图形对象(如图形上下文和笔)就像疯了一样,甚至没有尝试Dispose任何一个。由于其中一些是稀缺的资源,它们会在实际内存之前耗尽,垃圾收集器不会被踢进去并为你摆脱它们。在RAM内存耗尽之前很久就会出现内存不足错误。尝试确保每当你创建一个实现IDisposable的对象时,你完成它就调用Dispose方法(通过Dispose方法显式,或隐式使用使用 block)。
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).


这篇关于在运行时绘制椭圆并使用widen metod。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆