2012-02-28 168 views
3

我需要作物图像不改变宽高比。我正在使用EDSDK从CANON1100D拍摄照片。捕获的图像: 宽度= 1920高度= 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)。

我该如何编程?任何想法

+1

我不确定我是否正确阅读了您的条件。您需要将宽高比从1:1.5更改为1:1.333,但不更改宽高比? – 2012-02-28 07:36:24

+0

我需要**裁剪**图像的一部分,纵横比为1:1.5的原始图像的纵横比为1:1.33 – 2012-02-28 08:05:49

回答

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

也许这样的事?

source

+0

您是否可以解释参数cropX,cropY? – 2012-02-28 08:17:13

+0

cropX和cropY是用于裁剪图像的矩形的原点 – TomP89 2012-02-28 08:44:06

+1

开始裁剪的位置。 x = 0和y = 0应该从左上角开始。图像上的一个点。 – 2012-02-28 08:44:45

2

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


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; 
} 
相关问题