在按钮上的图片框上重新创建椭圆以单击 [英] Recreate ellipse over picturebox on butto click

查看:40
本文介绍了在按钮上的图片框上重新创建椭圆以单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我已经在图片框上创建了多个椭圆并存储了点,现在,我将基于存储的点在Button单击上重新创建所有椭圆.但是我的recreate函数无法正常工作.它没有显示任何椭圆.

请帮帮我,

我的代码在这里:


Dear Friends,

I have created mutiple ellipse over a picture box and stored points, Now I am recreating all ellipse on a Button click based on stored points. But my recreate function is not working fine.It is not showing any ellipse.

Please Help me out ,

My code is here:


private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
         //   Point mouseDownLocation = pictureBox1.PointToClient(new Point(e.X, e.Y));
            mRect = new Rectangle(e.X, e.Y, 0, 0);

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mRect = new Rectangle(mRect.Left, mRect.Top, e.X - mRect.Left, e.Y - mRect.Top);
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            Color c1 = Color.FromArgb(50, Color.Green);

            string ss = mRect.X.ToString() + "," + mRect.Y.ToString() + "," + mRect.Width.ToString() + "," + mRect.Height.ToString();
            //Draw a rectangle with 2pixel wide line
            using (Pen pen = new Pen(Color.Red, 2))
            {
                Graphics myGraphics = pictureBox1.CreateGraphics();
                myGraphics.DrawEllipse(pen, mRect);
                myGraphics.FillEllipse(new SolidBrush(c1), mRect);
                listBox1.Items.Add(ss);
                
            }
        }

private void btnSaveObject_Click(object sender, EventArgs e)
        {
           
            recreate();
        }
        private void recreate()
        {
            
            pictureBox1.Invalidate();
            foreach (string str in listBox1.Items)
            {
                string[] strArr = str.Split(new Char[] { ',' });
                using (Pen pen = new Pen(Color.Red, 2))
                {
                    Graphics myGraphics = pictureBox1.CreateGraphics();
                    myGraphics.DrawEllipse(pen, Convert.ToInt32(strArr[0]), Convert.ToInt32(strArr[1]), Convert.ToInt32(strArr[2]), Convert.ToInt32(strArr[3]));
                   
                    myGraphics.Dispose();
                    pictureBox1.Refresh();
                }
            }
        }

推荐答案

删除该行:
Remove the line:
pictureBox1.Refresh();

它使PictureBox从头开始重绘,扔掉您刚才绘制的椭圆...

更好的是,不要在鼠标或按钮处理程序中绘制任何椭圆形-处理PictureBox.Paint事件并在那里进行处理.使用PictureBox.Invalidate()使其重绘-如果不这样做,则将表格最小化或将其他表格放在顶部时,它们将消失

It causes the PictureBox to redraw from scratch, throwing away the ellipses you just drew...

Better, don''t draw any of the ellipses in the mouse or button handlers - handle the PictureBox.Paint event and do them there. Use PictureBox.Invalidate() to cause it to redraw them - if you don''t then they will disappear if you minimise the form, or put another form over the top


实际上,省略号被绘制,但最后一条语句pictureBox1.Refresh();立即将其删除.

但是,正确的方法是根本不在MouseUp事件中绘制. MouseUp应该仅限于存储必要的数据并调用pictureBox1.Invalidate();.

然后,绘图将在pictureBox1_Paint中进行.将该方法订阅到pictureBox1的Paint事件.在该方法中,请按照在Recreate()方法中尝试过的方法进行操作,但不要刷新和无效.

Paint方法将以PaintEventArgs作为第二个参数.使用PaintEventArgs.Graphics对象而不是创建一个新对象.

稍后:
1.您不需要使用PictureBox进行绘制.这可以通过任何Control派生的类来实现.如果需要PictureBox来显示现有图片,请使用它.

2.不能在字符串中存储椭圆坐标,请使用 ^ ].
Actually, the ellipses get drawn, but immediately erased by your last statement: pictureBox1.Refresh();.

The correct way, however is not to draw at all in the MouseUp event. MouseUp should confine itself to storing the necessary data and calling pictureBox1.Invalidate();.

The drawing then will happen in pictureBox1_Paint. Subscribe that method to pictureBox1''s Paint event. In that method, do as you tried in your Recreate() method but without Refresh and Invalidate.

The Paint method will have a PaintEventArgs as second parameter. Use the PaintEventArgs.Graphics object rather than creating a new one.

For later:
1. You don''t need a PictureBox to draw on. That can be achieved with any Control-derived class. Use a PictureBox if you need its ability to show existing pictures.

2. Storing ellipse co-ordinates in a string is not the way to go, use a List<Rectangle>[^] instead.


这篇关于在按钮上的图片框上重新创建椭圆以单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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