| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2152 人关注过本帖
标题:剪切图片时,当鼠标弹起时,显示鼠标所画的矩形部分,怎样把这个用鼠标画的 ...
只看楼主 加入收藏
x251738507
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-7-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:6 
剪切图片时,当鼠标弹起时,显示鼠标所画的矩形部分,怎样把这个用鼠标画的矩形去掉
private Bitmap bt;
        private Bitmap bt1;
        Point p1;
        Point p2;
        bool b = false;
        Image img;

/// <summary>
        /// 剪切图片的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnShear_Click(object sender, EventArgs e)
        {
            b = true;
        }

        /// <summary>
        /// 获取按下鼠标指针的坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (b)
            {
                this.Cursor = Cursors.Cross;
                p1 = new Point(e.X, e.Y);
            }
        }

        /// <summary>
        /// 记录鼠标的移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (b)
            {
                if (this.Cursor == Cursors.Cross)
                {
                    p2 = new Point(e.X, e.Y);
                    pictureBox.Invalidate();
                }
            }
        }

        /// <summary>
        /// 获取松开鼠标时指针的坐标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (b)
            {
                this.Cursor = Cursors.Default;
                p2 = new Point(e.X, e.Y);
                CutImage();
                b = false;
            }
        }

        /// <summary>
        /// 记录鼠标在PictureBox上面画的矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (b)
            {
                Pen p = new Pen(Color.White, 1);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                Rectangle rect = new Rectangle(p1, new Size(p2.X-p1.X,p2.Y-p1.Y));
                e.Graphics.DrawRectangle(p, rect);
                p.Dispose();
            }
        }

        /// <summary>
        /// 剪切图片
        /// </summary>
        private void CutImage()
        {
            try
            {
                img = this.pictureBox.Image;
                bt1 = new Bitmap(p2.X - p1.X, p2.Y - p1.Y);
                Rectangle tgtRect = new Rectangle(0, 0, bt1.Width, bt1.Height);
                Rectangle srcRect = new Rectangle(p1.X, p1.Y, bt1.Width, bt1.Height);
                Graphics g = Graphics.FromImage(bt1);
                g.DrawImage(img, tgtRect, srcRect, GraphicsUnit.Pixel);
                this.pictureBox.Image = bt1;
                g.Dispose();
                pictureBox.Refresh();
            }
            catch(Exception)
            {

            }
        }

当剪切后为什么用pen画的矩形还在,怎么去掉?
搜索更多相关主题的帖子: 剪切图片pen矩形 
2009-07-27 14:47
yangglemu
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:273
专家分:521
注 册:2006-9-15
收藏
得分:0 
代码太长,分又少,不看

试着挣个威望!
2009-07-27 16:00
baikil
Rank: 10Rank: 10Rank: 10
来 自:X星球
等 级:青峰侠
威 望:4
帖 子:282
专家分:1580
注 册:2009-7-20
收藏
得分:0 
1.把  private void pictureBox_Paint(object sender, PaintEventArgs e) 去掉,不用paint事件.
2. private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
        if (b)
            {
                pictureBox.Refresh();//清除所画的
                this.Cursor = Cursors.Default;
                p2 = new Point(e.X, e.Y);
                CutImage();
                b = false;
            }
        }
3.在这个
 private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (b)
            {
pictureBox.Refresh();//清除所画的
                if (this.Cursor == Cursors.Cross)
                {
                    p2 = new Point(e.X, e.Y);
                    pictureBox.Invalidate();
                }
                //添加一个画图功能
  
                Pen p = new Pen(Color.White, 1);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                Rectangle rect = new Rectangle(p1, new Size(e.X-p1.X,e.Y-p1.Y));
                e.Graphics.DrawRectangle(p, rect);
                p.Dispose();
            }
        }
完成!

[[it] 本帖最后由 baikil 于 2009-7-27 16:19 编辑 [/it]]

自创QQ群64885635,方便C#学习交流.希望各项工作路好学都好入!
2009-07-27 16:14
x251738507
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-7-25
收藏
得分:0 
回复 3楼 baikil
在pictureBox_MouseMove里面, e.Graphics.DrawRectangle(p, rect);        e为什么点不出来啊,是不是参数不一样
2009-07-27 16:27
baikil
Rank: 10Rank: 10Rank: 10
来 自:X星球
等 级:青峰侠
威 望:4
帖 子:282
专家分:1580
注 册:2009-7-20
收藏
得分:20 
建意还为
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (b)
            {
pictureBox.Refresh();//清除所画的
                if (this.Cursor == Cursors.Cross)
                {
                    p2 = new Point(e.X, e.Y);
                    pictureBox.Invalidate();
                }
                //添加一个画图功能
  
     using (Graphics g = pictureBox.CreateGraphics())
            {               
                 Pen p = new Pen(Color.White, 1);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                Rectangle rect = new Rectangle(p1, new Size(e.X-p1.X,e.Y-p1.Y));
                g.DrawRectangle(p, rect);
                p.Dispose();
            }
        }
    }

自创QQ群64885635,方便C#学习交流.希望各项工作路好学都好入!
2009-07-27 16:41
x251738507
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-7-25
收藏
得分:0 
回复 5楼
非常非常非常感谢baikil大哥的帮助
2009-07-27 16:48
x251738507
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2009-7-25
收藏
得分:0 
刷新的太频繁了,还是不行
2009-07-28 09:30
快速回复:剪切图片时,当鼠标弹起时,显示鼠标所画的矩形部分,怎样把这个用鼠标 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.063891 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved