4

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 ratio is 1.5. But I need picture which aspect ratio will be 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'd like to crop the image (1920x1280) to (1600x1200) in runtime.

How can I do this programmatically?

Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
  • 1
    I'm not sure I read your conditions correctly. You need to change the aspect ratio from 1:1.5 to 1:1.333, but without changing the aspect ratio? – Mr Lister Feb 28 '12 at 07:36
  • i need to **crop** a part of image with aspect ration 1:1.33 from the original image of aspect ratio 1:1.5 – Nikson Kanti Paul Feb 28 '12 at 08:05

2 Answers2

4
 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

Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51
auo
  • 572
  • 1
  • 7
  • 16
3

this is my solution for centered cropping.


Bitmap CenterCrop(Bitmap srcImage, int newWidth, int newHeight)
{
     Bitmap ret = null;

     int w = srcImage.Width;
     int h = srcImage.Height;

     if ( w < newWidth || h < newHeight)
     {
           MessageBox.Show("Out of boundary");
           return ret;
     }

     int posX_for_centerd_crop = (w - newWidth) / 2;
     int posY_for_centerd_crop = (h - newHeight) / 2;

     var CenteredRect = new Rectangle( posX_for_centerd_crop, 
                             posY_for_centerd_crop,  newWidth, newHeight);

     ret = srcImage.Clone(imageCenterRect, srcImage.PixelFormat);

     return ret;
}
Nikson Kanti Paul
  • 3,394
  • 1
  • 35
  • 51