C#实现图像的鼠标裁剪

C#的图像裁剪很容易操作,这里给个实现的例子

关键是需要处理鼠标的事件和一些更新

实现鼠标移动的代码.注意更新不要全部重画,只有选择矩形部分重画

  private void Form1_MouseMove(object sender, MouseEventArgs e)
        {

            if (Track_move)
                endpoint = new Point(e.X, e.Y);
            else
            {
                return;
            }
            rect1 = new Rectangle(stpoint.X, stpoint.Y, endpoint.X - stpoint.X, endpoint.Y - stpoint.Y);

            Rectangle tempr = new Rectangle(rect1.X, rect1.Y, rect1.Width + 2, rect1.Height + 2);
            this.Invalidate(tempr);
        }

选择结束的处理代码.

  private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && Track_move==true )
            {
                Track_move = false;
                endpoint = new Point(e.X, e.Y);
                rect1 = new Rectangle(stpoint.X, stpoint.Y, endpoint.X - stpoint.X, endpoint.Y - stpoint.Y);
                Rectangle rectorg = new Rectangle(borg.X, borg.Y, image1.Width, image1.Height);
                if (rect1.Width <= 0)
                    return;
                if (rect1.Height <= 0)
                    return;
                if (rectorg.Contains(rect1))
                {
                    Rectangle rectadj = new Rectangle(rect1.X - borg.X, rect1.Y - borg.Y, rect1.Width, rect1.Height);
                    Bitmap cropimge = image1.Clone(rectadj, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    pictureBox2.Image = cropimge;
                }
                else
                {
                    pictureBox2.Image = null;
                }
                this.Invalidate();
            }
        }

程序的整个代码

using System;
using System.Collections.Generic;
using System.Comp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值