c#抠图并保存

 

 

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

posted @ 2016-11-28 10:37  小石头的博客园  阅读(2935)  评论(0编辑  收藏  举报