/// <summary> /// 按指定大小裁切图片,如果指定大小超过原始图片,将按原始图片尺寸输出 /// </summary> /// <param name="sourceImagePath">原始图片地址</param> /// <param name="newImagePath">新图片地址</param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="imageFormat"></param> /// <param name="position">裁切开始位置</param> /// <returns></returns> public static bool CutImage(string sourceImagePath, string newImagePath, int width, int height, ImageFormat imageFormat, CutImagePosition position) { using (FileStream fs = new FileStream(sourceImagePath, FileMode.Open)) { return(CutImage(fs, newImagePath, width, height, imageFormat, position)); } }
/// <summary> /// 按指定大小裁切图片,如果指定大小超过原始图片,将按原始图片尺寸输出 /// </summary> /// <param name="sourceStream">原始图片流</param> /// <param name="newImagePath">新图片地址</param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="imageFormat"></param> /// <param name="position">裁切开始位置</param> /// <returns></returns> public static bool CutImage(Stream sourceStream, string newImagePath, int width, int height, ImageFormat imageFormat, CutImagePosition position) { bool result = false; try { string newImageDirPath = Path.GetDirectoryName(newImagePath); if (!Directory.Exists(newImageDirPath)) { Directory.CreateDirectory(newImageDirPath); } string newImageTempPath = Path.Combine(newImageDirPath, Path.GetFileNameWithoutExtension(newImagePath) + "_temp_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Path.GetExtension(newImagePath)); int orgWidth = 0, orgHeight = 0; using (Image img = Image.FromStream(sourceStream)) { orgWidth = img.Width; orgHeight = img.Height; if (width >= orgWidth && height >= orgHeight) { using (FileStream fs = new FileStream(newImagePath, FileMode.OpenOrCreate)) { byte[] sourceBuffer = new byte[sourceStream.Length]; fs.Write(sourceBuffer, 0, sourceBuffer.Length); } return(true); } else { if (width > orgWidth) { width = orgWidth; } if (height > orgHeight) { height = orgHeight; } } } //获取系统编码类型数组,包含了jpeg,bmp,png,gif,tiff ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo i in icis) { if (i.FormatID == imageFormat.Guid) { ici = i; break; } } EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100); using (Bitmap bitmap = new Bitmap(width, height)) { bitmap.Save(newImageTempPath, ici, ep); } int x, y; switch (position) { case CutImagePosition.TopLeft: default: x = 0; y = 0; break; case CutImagePosition.TopCenter: x = Convert.ToInt32((orgWidth - width) / 2); y = 0; break; case CutImagePosition.TopRight: x = (orgWidth - width); y = 0; break; case CutImagePosition.MiddleRight: x = (orgWidth - width); y = Convert.ToInt32((orgHeight - height) / 2); break; case CutImagePosition.BottomRight: x = (orgWidth - width); y = (orgHeight - height); break; case CutImagePosition.BottomCenter: x = Convert.ToInt32((orgWidth - width) / 2); y = (orgHeight - height); break; case CutImagePosition.BottomLeft: x = 0; y = (orgHeight - height); break; case CutImagePosition.MiddleLeft: x = 0; y = Convert.ToInt32((orgHeight - height) / 2); break; case CutImagePosition.Top: x = 0; y = 0; break; case CutImagePosition.Left: x = 0; y = 0; break; case CutImagePosition.Right: x = (orgWidth - width); y = 0; break; case CutImagePosition.Bottom: x = 0; y = (orgHeight - height); break; } Graphics g = null; using (Image tempImg = Image.FromFile(newImageTempPath)) { using (Image img = Image.FromStream(sourceStream)) { g = Graphics.FromImage(tempImg); g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel); g.Save(); } tempImg.Save(newImagePath); } g.Dispose(); File.Delete(newImageTempPath); result = true; } catch (Exception ex) { LogHelper.WriteLog_LocalTxt(ex.ToString()); result = false; } return(result); }
/// <summary> /// 按百分比裁切图片 /// </summary> /// <param name="sourceStream">原始图片流</param> /// <param name="newImagePath">新图片地址</param> /// <param name="percentage">百分比</param> /// <param name="imageFormat"></param> /// <param name="position">裁切开始位置</param> /// <returns></returns> public static bool CutImage(Stream sourceStream, string newImagePath, double percentage, ImageFormat imageFormat, CutImagePosition position) { int orgWidth = 0, orgHeight = 0; int newWidth = 0, newHeight = 0; using (Image img = Image.FromStream(sourceStream)) { orgWidth = img.Width; orgHeight = img.Height; switch (position) { case CutImagePosition.Top: case CutImagePosition.Bottom: newWidth = orgWidth; newHeight = Convert.ToInt32(orgHeight * percentage); break; case CutImagePosition.Left: case CutImagePosition.Right: newWidth = Convert.ToInt32(orgWidth * percentage); newHeight = orgHeight; break; default: newWidth = Convert.ToInt32(orgWidth * percentage); newHeight = Convert.ToInt32(orgHeight * percentage); break; } } return(CutImage(sourceStream, newImagePath, newWidth, newHeight, imageFormat, position)); }