2016-12-31 102 views
0

我有一個形象,而我只需要裁剪這如何設置寬度和高度圖像C#

  • 比如我的圖像的像素爲(257,50)
  • 我只是需要(200, 20)那張圖片我不需要那張圖片的其餘部分 我該怎麼做?
  • List item
+4

http://stackoverflow.com/questions/734930/how-to-crop-an-image-using-c –

+0

會嘗試這個功能你想裁剪它還是隻顯示它的一部分? –

+0

@TalhaTalipAçıkgöz是 – amir

回答

1

您可以從How to crop/resize image

private Bitmap CropImage(Image originalImage, Rectangle sourceRectangle, 
        Rectangle? destinationRectangle = null) 
    { 
     if (destinationRectangle == null) 
     { 
      destinationRectangle = new Rectangle(Point.Empty, sourceRectangle.Size); 
     } 

     var croppedImage = new Bitmap(destinationRectangle.Value.Width, 
      destinationRectangle.Value.Height); 
     using (var graphics = Graphics.FromImage(croppedImage)) 
     { 
      graphics.DrawImage(originalImage, destinationRectangle.Value, 
       sourceRectangle, GraphicsUnit.Pixel); 
     } 
     return croppedImage; 
    } 

    /// <summary> 
    /// Button click to choose an image and test 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void btnCrop_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog ofd = new OpenFileDialog(); 
     if (ofd.ShowDialog() == DialogResult.OK) 
     { 
      string imageFile = ofd.FileName; 

      Image img = new Bitmap(imageFile); 
      Rectangle source = new Rectangle(0, 0, 120, 20); 
      Image cropped = CropImage(img, source); 
      // Save cropped image here 
      cropped.Save(Path.GetDirectoryName(imageFile) + "\\croppped." + Path.GetExtension(imageFile)); 
     } 
    } 
相關問題