在C#中 从一个picturebox中 按住鼠标左键不放 画一块区域后,另外一个picturebox(在一个winform 窗体上有两个picturebox )上马上把对应的截取图像显示出来

        private int _x, _y;
        Image _img
= null ;
       
private void pictureBox1_MouseDown( object sender, MouseEventArgs e)
        {
            _img
= pictureBox1.Image.Clone() as Image;
            _x
= e.X;
            _y
= e.Y;

        }

       
private void pictureBox1_MouseUp( object sender, MouseEventArgs e)
        {
           
int sx = _x < e.X ? _x : e.X;
           
int sy = _y < e.Y ? _y : e.Y;
           
int w = Math.Abs(_x - e.X);
           
int h = Math.Abs(_y - e.Y);
            Graphics g
= Graphics.FromHwnd(pictureBox2.Handle);
            g.Clear(pictureBox2.BackColor);
            g.DrawImage(pictureBox1.Image,
new Rectangle( 0 , 0 , w, h), sx, sy, w, h, GraphicsUnit.Pixel);
        }

       
private void pictureBox1_MouseMove( object sender, MouseEventArgs e)
        {
           
if (e.Button == MouseButtons.Left)
            {
                Pen p
= new Pen(Color.Red, 1 );
                p.DashStyle
= System.Drawing.Drawing2D.DashStyle.Dash;
                Graphics g
= Graphics.FromHwnd(pictureBox1.Handle);
               
int sx = _x < e.X ? _x : e.X;
               
int sy = _y < e.Y ? _y : e.Y;
               
int w = Math.Abs(_x - e.X);
               
int h = Math.Abs(_y - e.Y);
                pictureBox1.Image
= _img;
                g.DrawRectangle(p, sx, sy, w, h);
            }
        }


不过这样效果不是很好,最好是这样,你有一个image保存最原始的图片, 画虚线框在这个image的副本上画,然后赋值给pictuebox1,取图片拷贝的时候也从原始的image上取
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你好,下面是一个WinForm 应用使用 PictureBox 显示矩形选择器的示例代码: 1. 首先,在 Form 添加一个 PictureBox 控件。 2. 然后在 PictureBox 的 MouseDown、MouseMove、MouseUp 事件实现矩形选择器操作,示例代码如下: ```csharp private bool _isDrawing = false; private Point _startPoint; private Point _endPoint; private Rectangle _rect; private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { _isDrawing = true; _startPoint = e.Location; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { if (_isDrawing) { _endPoint = e.Location; _rect = new Rectangle( Math.Min(_startPoint.X, _endPoint.X), Math.Min(_startPoint.Y, _endPoint.Y), Math.Abs(_startPoint.X - _endPoint.X), Math.Abs(_startPoint.Y - _endPoint.Y) ); pictureBox1.Invalidate(); // 绘制矩形 } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (_isDrawing) { _isDrawing = false; // TODO: 处理选区域 } } private void pictureBox1_Paint(object sender, PaintEventArgs e) { if (_isDrawing) { ControlPaint.DrawFocusRectangle(e.Graphics, _rect); } } ``` 3. 最后,在 Form 的 Load 事件添加如下代码,让 PictureBox 较好的支持绘制操作: ```csharp private void Form1_Load(object sender, EventArgs e) { pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; // 保持图片原始大小 pictureBox1.BackColor = Color.Black; // 设置背景色 pictureBox1.BorderStyle = BorderStyle.FixedSingle; // 设置边框 pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint); // 添加绘图事件 pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown); // 添加鼠标事件 pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove); pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp); } ``` 这样,当您按下键并移动鼠标时,将在图片上绘制矩形。当您释放鼠标按钮时,您可以处理选区域
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值