[英]How to Crop Image without changing the aspect ratio
我需要裁剪图像而不改变其纵横比。 我正在使用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)。
我该如何以编程方式执行此操作? 任何的想法
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");
}
也许是这样的?
这是我对中心裁剪的解决方案。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.