模仿QQ截图片

本文介绍了一个使用 C# 实现的 PictureBox 图片裁剪功能。通过鼠标操作可以在PictureBox中选择一个矩形区域,并将该区域内的图片裁剪后放置到另一个PictureBox中。此功能适用于图像编辑应用程序。
摘要由CSDN通过智能技术生成
两个picturebox,一个放图片

完整代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Xml;
namespace Test
{
    public partial class Form1 : Form
    {
        private bool MouseIsDown = false;
        private int _x, _y;
        private Rectangle MouseRect = Rectangle.Empty;
        public Form1()
        {
            InitializeComponent();
        }
        private void ResizeToRectangle(Point p)
        {
            DrawRectangle();
            MouseRect.Width = p.X - MouseRect.Left;
            MouseRect.Height = p.Y - MouseRect.Top;
            DrawRectangle();
        }
        private void DrawRectangle()
        {
            Rectangle rect = this.RectangleToScreen(MouseRect);
            ControlPaint.DrawReversibleFrame(rect, Color.White, FrameStyle.Dashed);

        }
        private void DrawStart(Point StartPoint)
        {
            barkPictureBox.Capture = true;
            Cursor.Clip = this.RectangleToScreen(new Rectangle(0, 0, ClientSize.Width, ClientSize.Height));
            MouseRect = new Rectangle(StartPoint.X, StartPoint.Y, 0, 0);
        }


        private void button1_Click(object sender, EventArgs e)
        {

        }


        void barkPictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            barkPictureBox.Capture = false;
            Cursor.Clip = Rectangle.Empty;
            MouseIsDown = false;
            DrawRectangle();

            if (MouseRect.X == 0 || MouseRect.Y == 0 || MouseRect.Width == 0 || MouseRect.Height == 0)  //如果区域有一个是0就返回
            {
            }
            else
            {
                ///代码
            }

            MouseRect = Rectangle.Empty;
            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(barkPictureBox.Image, new Rectangle(0, 0, w, h), sx, sy, w, h, GraphicsUnit.Pixel); 
        }

        void barkPictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseIsDown) ResizeToRectangle(new Point(e.X + barkPictureBox.Location.X, e.Y + barkPictureBox.Location.Y));
           
        }

        void barkPictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            _x = e.X;
            _y = e.Y;

            MouseIsDown = true;
            DrawStart(new Point(e.X + barkPictureBox.Location.X, e.Y + barkPictureBox.Location.Y));
        }


 

   

        private void Form1_Load(object sender, EventArgs e)
        {
            barkPictureBox.MouseDown += new MouseEventHandler(barkPictureBox_MouseDown);
            barkPictureBox.MouseMove += new MouseEventHandler(barkPictureBox_MouseMove);
            barkPictureBox.MouseUp += new MouseEventHandler(barkPictureBox_MouseUp);

        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值