C# – How to Crop Image without changing the aspect ratio

cedsdkemgucvimage processing

I need to crop an image without changing its aspect ratio. I am taking picture from CANON1100D using EDSDK. Captured image:
Width = 1920 and Height=1280
Aspect ration is 1.5. but I need picture which aspect ratio is 1.33.


// convert into processing resolution (1600,1200) 

Image<Bgr, byte> runtime_frm = new Image<Bgr, byte>(frame.ToBitmap(1600,1200));

// also in bitmap processing 

// Bitmap a = new Bitmap(runtime_frm.ToBitmap());  
// Bitmap b = new Bitmap(a, new Size(1600,1200));

It's resizing the image so the aspect ratio of image is changed but it creates stress in image. I need to crop the image (1920×1280) to (1600×1200) in runtime.

How can i do this programmatically? any idea

Best Answer

 public void Crop(Bitmap bm, int cropX, int cropY,int cropWidth,int cropHeight)
 {
       var rect = new System.Drawing.Rectangle(cropX,cropY,cropWidth,cropHeight);

       Bitmap newBm = bm.Clone(rect, bm.PixelFormat);

       newBm.Save("image2.jpg");
 }

Maybe something like that?

source