C#中pictureBox中从中间开始截取图片

2025-03-04 07:22:51
推荐回答(2个)
回答1:

看要求是从中轴线向两边截取指定宽度图片了

//截图图片
private Image cropImage(int imgWidth)
{
// READ IMAG
Image imgSource= Image.FromFile(filepath);
//取原图片宽,高
double orgWidth = Int2Double(imgSource.Width);
double orgHeight = Int2Double(imgSource.Height);

//声明图片区域
Rectangle cropArea = new Rectangle();

//设定左上角点坐标
double x = orgWidth / 2 - imgWidth;
double y = 0;
//计算截取区域宽,高
double width = imgWidth*2;
double height = orgHeight;

//设定图片区域参数
cropArea.X = Double2Int(x);
cropArea.Y = Double2Int(y);
cropArea.Width = Double2Int(width);
cropArea.Height = Double2Int(height);
//截取图片
Bitmap bmpImage = new Bitmap(imgSource);
Bitmap bmpCrop = bmpImage.Clone(cropArea,
bmpImage.PixelFormat);

return bmpCrop;
}

private double Int2Double(int i)
{
return double.Parse(i.ToString());
}

private int Double2Int(double d)
{
return int.Parse(Math.Round(d, 0).ToString());
}

回答2:

网上有狠多例子啊。修改下就可以用了