c# 剪裁图片
剪裁图片
/// <summary> /// 剪裁图片 /// </summary> /// <param name="src">原图片</param> /// <param name="left">左坐标</param> /// <param name="top">顶部坐标</param> /// <param name="right">右坐标</param> /// <param name="bottom">底部坐标</param> /// <returns>剪裁后的图片</returns> public static Image CutImage(Image src, int left, int top, int right, int bottom) { Bitmap srcBitmap = new Bitmap(src); int width = right - left; int height = bottom - top; Bitmap destBitmap = new Bitmap(width, height); using (Graphics g = Graphics.FromImage(destBitmap)) { g.Clear(Color.Transparent); //设置画布的描绘质量 g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(srcBitmap, new Rectangle(0, 0, width, height), left, top, width, height, GraphicsUnit.Pixel); } return destBitmap; }
根据图片路径读取图片
/// <summary> /// 根据图片路径读取图片 /// </summary> /// <param name="imageUrl"></param> /// <returns></returns> public static Image ReadFromFile(string imageUrl) { Image img = null; FileStream fs = null; try { fs = new FileStream(imageUrl, FileMode.Open, FileAccess.Read); img = Image.FromStream(fs); } finally { fs.Close(); } return img; }