剪切图像

实现效果:

  

知识运用:

  Bitmap类的Clone方法        //创建Bitmap对象的 某个部分 的副本

  public Bitmap Clone  (Rectangle rect, PixelFormat format)  //由Rectangle结构和指定的PixelFormat枚举定义

  Graphics类的FillRectangle方法    //填充Rectangle结构指定的矩形的内部

  public void FillRectangle (Brush brush, Rectangle rect)

实现代码:

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                    startPt = new Point(e.X, e.Y);
            }
        }

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

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                Graphics g = this.CreateGraphics();
                Bitmap bmp = new Bitmap(pictureBox1.Image);
                Bitmap cutBmp = bmp.Clone(rectangle,System.Drawing.Imaging.PixelFormat.DontCare);
                g.DrawImage(cutBmp,e.X,e.Y);
                SolidBrush myBrush = new SolidBrush(Color.Wheat);
                ig.FillRectangle(myBrush,rectangle);
            }
            catch{}
        }

  

猜你喜欢

转载自www.cnblogs.com/feiyucha/p/10261721.html