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