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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM