No 60 · 实现剪切图片

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Ex13_04
{
    public partial class Form1 : Form
    {
        bool isDrag = false;
        Rectangle theRectangle = new Rectangle(new Point(0, 0), new Size(0, 0));
        Point startPoint, oldPoint;//保存剪切的起始点坐标
        private Graphics ig;
        Bitmap MyBitmap;//保存原图片
        Bitmap cloneBitmap;//保存被剪切的图片
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //打开图像文件
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图像文件(JPeg, Gif)|*.jpg;*.jpeg;*.gif;| JPeg 图像文件(*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 图像文件(*.gif)|*.gif";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                //得到原始大小的图像
                MyBitmap = new Bitmap(openFileDialog.FileName);
            }
            this.pictureBox1.Image = MyBitmap;
            pictureBox1.Height = MyBitmap.Height;
            pictureBox1.Width = MyBitmap.Width;
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {

                //如果开始绘制,则开始记录鼠标位置
                if ((isDrag = !isDrag) == true)
                {
                    startPoint = new Point(e.X, e.Y);
                    oldPoint = new Point(e.X, e.Y);
                }
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            isDrag = false;
            ig = pictureBox1.CreateGraphics();
            ig.DrawRectangle(new Pen(Color.Black, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
            theRectangle = new Rectangle(startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics g = this.CreateGraphics();
            if (isDrag)
            {
                g.DrawRectangle(new Pen(Color.Black, 1), startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            pictureBox2.Refresh();
            Bitmap bitmap = new Bitmap(pictureBox1.Image);
            cloneBitmap = bitmap.Clone(theRectangle, PixelFormat.DontCare);
            Graphics g = pictureBox1.CreateGraphics();
            SolidBrush myBrush = new SolidBrush(Color.White);
            g.FillRectangle(myBrush, theRectangle);            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Graphics g = this.pictureBox2.CreateGraphics();
            g.DrawImage(cloneBitmap,0,0);
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            Graphics g = pictureBox1.CreateGraphics();
            SolidBrush myBrush = new SolidBrush(Color.White);
            g.FillRectangle(myBrush, theRectangle);
        }
    }
}

 

你可能感兴趣的:(No 60 · 实现剪切图片)