C#画矩形,如何擦掉原来的痕迹

楚楚3107 2017-04-25 05:01:13
就是有好多重绘的矩形,怎么消掉他啊
...全文
451 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
楚楚3107 2017-04-26
  • 打赏
  • 举报
回复
引用 8 楼 xuzuning 的回复:
其实你搜索 橡皮筋 会有好几个方案的
谢谢了
xuzuning 2017-04-26
  • 打赏
  • 举报
回复
其实你搜索 橡皮筋 会有好几个方案的
xuzuning 2017-04-26
  • 打赏
  • 举报
回复
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            pictureBox1.MouseDown+=new MouseEventHandler(pictureBox1_MouseDown);
            pictureBox1.MouseMove+=new MouseEventHandler(pictureBox1_MouseMove);
            pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
        }
 
        private Rectangle m_MouseRect = Rectangle.Empty;
        public delegate void SelectRectangel(object sneder, Rectangle e);
        public event SelectRectangel SetRectangel;
        private bool m_MouseIsDown = false;
        
        int intwidth = 0;
        int intheigh = 0;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            m_MouseIsDown = true;
            DrawStart(new Point(e.X, e.Y));
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_MouseIsDown) ResizeToRectangle(new Point(e.X, e.Y));
        }
        float w = 0;//宽度比例
        float h = 0;//高度比例

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            w = (float)intwidth / (float)pictureBox1.Width;
            h = (float)intheigh / (float)pictureBox1.Height;
            Cursor.Clip = Rectangle.Empty;
            m_MouseIsDown = false;
            DrawRectangle();
            if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0)
            {
                //如果区域没0 就不执行委托 
            }
            else
            {
                if (SetRectangel != null) SetRectangel(pictureBox1, m_MouseRect);
            }
            DrawRectangle();

            //ControlPaint.DrawReversibleFrame(pictureBox1.RectangleToScreen(m_MouseRect), Color.Red, FrameStyle.Thick);
        }

        /// <summary> 
        /// 刷新绘制 
        /// </summary> 
        /// <param name="p"></param> 
        private void ResizeToRectangle(Point p_Point)
        {
            DrawRectangle();
            m_MouseRect.Width = p_Point.X - m_MouseRect.Left;
            m_MouseRect.Height = p_Point.Y - m_MouseRect.Top;
            DrawRectangle();
        }

        /// <summary> 
        /// 绘制区域 
        /// </summary> 
        private void DrawRectangle()
        {
            Rectangle _Rect = pictureBox1.RectangleToScreen(m_MouseRect);
            ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
        }

        /// <summary> 
        /// 开始绘制 并且设置鼠标区域 
        /// </summary> 
        /// <param name="StartPoint">开始位置</param> 
        private void DrawStart(Point p_Point)
        {
            Cursor.Clip = pictureBox1.RectangleToScreen(new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);
        }
    }
楚楚3107 2017-04-26
  • 打赏
  • 举报
回复
引用 5 楼 xuzuning 的回复:
给代码呗,我差点要结帖。哈哈
xuzuning 2017-04-26
  • 打赏
  • 举报
回复
xuzuning 2017-04-25
  • 打赏
  • 举报
回复
在项目中截了一段,应该是完整的
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            m_MouseIsDown = true;
            DrawStart(new Point(e.X, e.Y));
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_MouseIsDown) ResizeToRectangle(new Point(e.X, e.Y));
        }
        float w = 0;//宽度比例
        float h = 0;//高度比例

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            w = (float)intwidth / (float)pictureBox1.Width;
            h = (float)intheigh / (float)pictureBox1.Height;
            Cursor.Clip = Rectangle.Empty;
            m_MouseIsDown = false;
            DrawRectangle();
            if (m_MouseRect.X == 0 || m_MouseRect.Y == 0 || m_MouseRect.Width == 0 || m_MouseRect.Height == 0)
            {
                //如果区域没0 就不执行委托 
            }
            else
            {
                if (SetRectangel != null) SetRectangel(pictureBox1, m_MouseRect);
            }

            ControlPaint.DrawReversibleFrame(pictureBox1.RectangleToScreen(m_MouseRect), Color.Red, FrameStyle.Thick);
            System.Drawing.Bitmap bmp = new Bitmap(pictureBox1.Image);
            Graphics m_mouse = Graphics.FromImage(bmp);
            Brush brush = new SolidBrush(Color.Red);
            Pen pen = new Pen(brush, 3);
            int wd = (int)(w * m_MouseRect.Width);
            int hd = (int)(h * m_MouseRect.Height);
            m_mouse.DrawEllipse(pen, new Rectangle((int)(m_MouseRect.X * w), (int)(h * m_MouseRect.Y), wd, hd));
            m_mouse.DrawRectangle(pen, new Rectangle((int)(m_MouseRect.X * w), (int)(h * m_MouseRect.Y), wd, hd));
            //MemoryStream ms = new MemoryStream();
            // bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            bmp.Save("D://1.bmp");
            pictureBox3.Image = bmp;

            //截屏可以实现效果
            Bitmap myImage = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(myImage);
            g.CopyFromScreen(pictureBox1.PointToScreen(pictureBox1.Location), new Point(0, 0), pictureBox1.PreferredSize);
            IntPtr dc1 = g.GetHdc();
            g.ReleaseHdc(dc1);
            pictureBox2.Image = (Image)myImage;
        }

        /// <summary> 
        /// 刷新绘制 
        /// </summary> 
        /// <param name="p"></param> 
        private void ResizeToRectangle(Point p_Point)
        {
            DrawRectangle();
            m_MouseRect.Width = p_Point.X - m_MouseRect.Left;
            m_MouseRect.Height = p_Point.Y - m_MouseRect.Top;
            DrawRectangle();
        }

        /// <summary> 
        /// 绘制区域 
        /// </summary> 
        private void DrawRectangle()
        {
            Rectangle _Rect = pictureBox1.RectangleToScreen(m_MouseRect);
            ControlPaint.DrawReversibleFrame(_Rect, Color.White, FrameStyle.Dashed);
        }

        /// <summary> 
        /// 开始绘制 并且设置鼠标区域 
        /// </summary> 
        /// <param name="StartPoint">开始位置</param> 
        private void DrawStart(Point p_Point)
        {
            Cursor.Clip = pictureBox1.RectangleToScreen(new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            m_MouseRect = new Rectangle(p_Point.X, p_Point.Y, 0, 0);
        }
荷塘散人 2017-04-25
  • 打赏
  • 举报
回复
public partial class Form1 : Form { Point startPoint = new Point(); //起始坐标 Point endPoint = new Point(); //结束坐标 bool isDrawing = false; public Form1() { InitializeComponent(); } private void Form1_MouseDown(object sender, MouseEventArgs e) { startPoint = e.Location; isDrawing = true; } private void Form1_MouseMove(object sender, MouseEventArgs e) { if(isDrawing) { endPoint = e.Location; this.Refresh(); } } private void Form1_MouseUp(object sender, MouseEventArgs e) { isDrawing = false; endPoint = e.Location; this.Refresh(); } private void Form1_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawRectangle(new Pen(Color.Red), new Rectangle(startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y)); } }
xuggzu 2017-04-25
  • 打赏
  • 举报
回复
思路肯定是先清除再画,具体做法要看你怎么画。
angel6709 2017-04-25
  • 打赏
  • 举报
回复
g.Clear(color)

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧