public int cutPicAndSave(string oldPath, string newPath, int x, int y, int width, int height)
{
Rectangle cropArea = new System.Drawing.Rectangle(x, y, width, height); //要截取的區域大小
Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(System.IO.File.ReadAllBytes(oldPath)));//加載圖片
//判斷超出的位置否
if ((img.Width < x + width) || img.Height < y + height)
{
//logger.Warn("截取的區域超過了圖片本身的高度、寬度.");
img.Dispose();
return 0;
}
Bitmap bmpImage = new System.Drawing.Bitmap(img);
Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
bmpCrop.Save(newPath);//保存成新文件
//釋放對象
img.Dispose();
bmpCrop.Dispose();
return 1;
}
string oriPicPath = Path.Combine(m_sharedFolders + this.param.PicOriPath, this.param.PicOriName);//原始圖片全路徑
string m_CutPicPath = System.IO.Path.GetExtension(oriPicPath);
m_CutPicPath = oriPicPath.Substring(0, oriPicPath.Length - m_CutPicPath.Length) + "_cut" + m_CutPicPath;//裁剪圖片全路徑
int bIsSave = cutPicAndSave(oriPicPath, m_CutPicPath, x, y, width, height);