3

我需要在不改变纵横比的情况下裁剪图像。我正在使用 EDSDK 从CANON1100D拍照。捕获的图像: Width = 1920Height=1280
纵横比为1.5。但我需要图片的宽高比为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));

它正在调整图像的大小,因此图像的纵横比发生了变化,但它会在图像中产生压力。我想在运行时将图像(1920x1280)裁剪为(1600x1200)。

如何以编程方式执行此操作?

4

2 回答 2

3
 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");
 }

也许是这样的?

来源

于 2012-02-28T07:19:58.810 回答
3

这是我的中心裁剪解决方案。


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;
}
于 2012-02-28T08:49:02.707 回答