2009-08-02 168 views
1

我想在c#中裁剪圖像。正如在大多數照片編輯軟件中,我想使用可以通過鼠標調整大小和重新定位的矩形框。另外,我想知道如何突出裁剪區域,如this photo所示。使用矩形裁剪圖片

+0

您在這裏找什麼?從較大的圖像裁剪一部分的實際方法?或者您可以通過一種方式顯示您的剪裁選擇,就像您鏈接的圖片一樣? – peSHIr 2009-08-02 20:31:02

+0

我已經對裁剪有一些想法。我想幫助您展示剪裁選擇的方式。我也移動裁剪選擇。 – qulzam 2009-08-03 01:25:13

回答

0

選擇框的外部似乎有一個黑色的圖像放置在它的約30%的alpha。爲此,您只需將內容區域外的每個像素都繪製成一個黑色像素,並在其上面繪製一個30%的alpha值。這會產生所需的暗淡效果。

至於如何獲得一個矩形在C#中動態分頁。

+0

感謝Nick Beradi尋求幫助。問題是,如果我一個接一個像素地工作,那就花很多時間。有沒有像顏色矩陣的快速方式? – qulzam 2009-08-03 01:27:48

0

爲了畫出更亮或更暗的圖片(或以任何方式更改顏色),請使用ColorMatrix,如this

+0

我知道使用顏色矩陣,但我知道只在所​​有圖像上使用顏色矩陣。我們怎樣才能在圖像的一部分使用它? – qulzam 2009-08-03 16:21:43

+0

將其繪製爲五個分離矩形(對.DrawImage的五個調用)。您可以將圖片分爲四個矩形,其中包含所有選區外的所有矩形和一個選區本身的矩形。 – 2009-08-04 08:52:50

0

您的圖像鏈接不再可用。

因此,假設在一個面板中你有你的圖片框與圖片裁剪。

首先,你需要創建事件處理程序的鼠標操作就能畫出您希望裁剪矩形區域:

private void picBox_MouseDown(object sender, MouseEventArgs e) 
    { 
     Cursor = Cursors.Default; 
     if (Makeselection) 
     { 
      try 
      { 
       if (e.Button == System.Windows.Forms.MouseButtons.Left) 
       { 
        Cursor = Cursors.Cross; 
        cropX = e.X; 
        cropY = e.Y; 

        cropPen = new Pen(Color.Crimson, 1); 
        cropPen.DashStyle = DashStyle.Solid; 
       } 
       picBox.Refresh(); 
      } 
      catch (Exception ex) 
      { 
      } 
     } 
    } 

    private void picBox_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (Makeselection) 
     { 
      Cursor = Cursors.Default; 
     } 
    } 

    private void picBox_MouseMove(object sender, MouseEventArgs e) 
    { 
     Cursor = Cursors.Default; 
     if (Makeselection) 
     { 
      picBox.Cursor = Cursors.Cross; 
      try 
      { 
       if (picBox.Image == null) 
        return; 


       if (e.Button == System.Windows.Forms.MouseButtons.Left) 
       { 
        picBox.Refresh(); 
        cropWidth = e.X - cropX; 
        cropHeight = e.Y - cropY; 
        picBox.CreateGraphics().DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight); 
       } 
      } 
      catch (Exception ex) 
      { 
      } 
     } 
    } 

    private void picBox_MouseLeave(object sender, EventArgs e) 
    { 
     tabControl.Focus(); 
    } 

    private void picBox_MouseEnter(object sender, EventArgs e) 
    { 
     picBox.Focus(); 
    } 

現在,自帶的按鈕點擊功能裁剪圖像:

private void btnCrop_Click_1(object sender, EventArgs e) 
    { 
     Cursor = Cursors.Default; 

     try 
     { 
      if (cropWidth < 1) 
      { 
       return; 
      } 
      Rectangle rect = new Rectangle(cropX, cropY, cropWidth, cropHeight); 
      //First we define a rectangle with the help of already calculated points 
      Bitmap OriginalImage = new Bitmap(picBoxScreenshot.Image, picBoxScreenshot.Width, picBoxScreenshot.Height); 
      //Original image 
      Bitmap _img = new Bitmap(cropWidth, cropHeight); 
      // for cropinfo image 
      Graphics g = Graphics.FromImage(_img); 
      // create graphics 
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; 
      g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 
      g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; 
      //set image attributes 
      g.DrawImage(OriginalImage, 0, 0, rect, GraphicsUnit.Pixel); 

      picBox.Image = _img; 
      picBox.Width = _img.Width; 
      picBox.Height = _img.Height; 
      PictureBoxLocation(); 
      cropWidth = 0; 
     } 
     catch (Exception ex){} 
    } 

private void PictureBoxLocation() 
    { 
     int _x = 0; 
     int _y = 0; 
     if (panel1.Width > picBox.Width) 
     { 
      _x = (panel1.Width - picBox.Width)/2; 
     } 
     if (panel1.Height > picBox.Height) 
     { 
      _y = (panel1.Height - picBox.Height)/2; 
     } 
     picBox.Location = new Point(_x, _y); 
     picBox.Refresh(); 
    }