如何在不破坏质量的情况下裁剪图像

时间:2019-05-21 13:39:17

标签: c# graphics imaging

我正在尝试在不破坏图像质量的情况下裁剪图像。以下代码搜索图像并将其显示在pictureBox1上。然后使用鼠标选择需要裁剪的图像区域。单击“裁剪”按钮时,所选区域将被裁剪并显示在pictureBox2中。

我遇到了一些解决方案,但是没有一个解决方案可以帮助我实现我想做的事情。有没有在不影响图像质量的情况下裁剪图像的方法?

Screenshot of image after it has been cropped

string sPID = null;
Boolean mouseClicked;
Point startPoint = new Point();
Point endPoint = new Point();
Rectangle rectCropArea;
Bitmap img;

private void btnSearch_Click(object sender, EventArgs e) 
{
      OpenFileDialog open = new OpenFileDialog();
      open.Filter = "Image files(*.jpg; *.jpeg; *.bmp; *.png;)|*.jpg; *.jpeg; *.bmp; *.png;";
      if (open.ShowDialog() == DialogResult.OK)
      {
          textBox2.Text = open.FileName;
          string path = Path.Combine(ConfigurationManager.AppSettings["searchpath"].ToString(),Path.GetFileName(textBox2.Text));
          img = new Bitmap(open.FileName);
          try
          {
               pictureBox1.Image = img;
          }
          catch (Exception ex)
          {
               MessageBox.Show(ConfigurationManager.
               AppSettings["error5"].ToString(),"Error Message");
          }
      } 
}

private void PicBox_MouseUp(object sender, MouseEventArgs e)
{
      mouseClicked = false;
      endPoint.X = -1;
      endPoint.Y = -1;
      startPoint.X = -1;
      startPoint.Y = -1;
}

private void PicBox_MouseDown(object sender, MouseEventArgs e)
{
      mouseClicked = true;
      startPoint.X = e.X;
      startPoint.Y = e.Y;
      endPoint.X = -1;
      endPoint.Y = -1;
      rectCropArea = new Rectangle(new Point(e.X, e.Y), new Size());
}   

private void PicBox_MouseMove(object sender, MouseEventArgs e)
{
      Point ptCurrent = new Point(e.X, e.Y);
      if (mouseClicked)
      {
          endPoint = ptCurrent;
      }    
      if (e.X > startPoint.X && e.Y > startPoint.Y)
      {
          rectCropArea.Width = e.X - startPoint.X;
          rectCropArea.Height = e.Y - startPoint.Y;
      }
      else if (e.X < startPoint.X && e.Y > startPoint.Y)
      {
          rectCropArea.Width = startPoint.X - e.X;
          rectCropArea.Height = e.Y - startPoint.Y;
          rectCropArea.X = e.X;
          rectCropArea.Y = startPoint.Y;
       }
       else if (e.X > startPoint.X && e.Y < startPoint.Y)
       {
          rectCropArea.Width = e.X - startPoint.X;
          rectCropArea.Height = startPoint.Y - e.Y;
          rectCropArea.X = startPoint.X;
          rectCropArea.Y = e.Y;
        }
        else
        {
           rectCropArea.Width = startPoint.X - e.X;
           rectCropArea.Height = startPoint.Y - e.Y;
           rectCropArea.X = e.X;
           rectCropArea.Y = e.Y;
        }
        pictureBox1.Refresh();
  }

  private void PicBox_MouseClick(object sender, MouseEventArgs e)
  {
        pictureBox1.Refresh();
  }

  private void PicBox_Paint(object sender, PaintEventArgs e)
  {
        Pen drawLine = new Pen(Color.Red);
        drawLine.DashStyle = DashStyle.Dash;
        e.Graphics.DrawRectangle(drawLine, rectCropArea);
  }

  private void btnCrop_Click(object sender, EventArgs e)
  {
        try
        {
           pictureBox3.Refresh();
           Bitmap sourceBitmap = new Bitmap(pictureBox1.Image, pictureBox1.Width, pictureBox1.Height);
           Bitmap cropImg = new Bitmap(rectCropArea.Width, rectCropArea.Height);
           Graphics g = Graphics.FromImage(cropImg);
           g.DrawImage(sourceBitmap, 0, 0, rectCropArea, GraphicsUnit.Pixel);
           pictureBox3.Image = cropImg;
        }
        catch
        {
        }
  }

0 个答案:

没有答案