C# - Crop Image Example

This code snippet shows how to crop an image.
  1. string fileName = "image.jpg";  
  2.  
  3. System.Drawing.Image img = System.Drawing.Image.FromFile(fileName);  
  4. Bitmap bmp = img as Bitmap;  
  5.  
  6. /** Crop the image, start from X=50, Y=50, Width=150, Height=150 **/  
  7. Bitmap cropBmp = bmp.Clone(new Rectangle(50, 50, 150, 150), bmp.PixelFormat);  
  8.  
  9. cropBmp.Save("new_file_name.jpg");  
  10.  
  11. cropBmp.Dispose();  
  12. bmp.Dispose();  
  13. img.Dispose()

No comments:

Post a Comment