C#图片截取压缩(百分比压缩/大小压缩)实现代码 前端时间朋友要传一些图片给我,全是大图,考虑到网速的限制,让他处理下图片大小再给我,这厮居然不知道用什么工具.为了娱乐写了个截取图片和压缩图片你的小工具 1.按照百分比截图 复制代码 代码如下: View Code /// /// 按照比例缩小图片 /// /// 要缩小的图片 /// 缩小比例 /// 缩小后的结果 public static Bitmap PercentImage(Image srcImage, double percent) { // 缩小后的高度 int newH = int.Parse(Math.Round(srcImage.Height * percent).ToString()); // 缩小后的宽度 int newW = int.Parse(Math.Round(srcImage.Width * percent).ToString()); try { // 要保存到的图片 Bitmap b = new Bitmap(newW, newH); Graphics g = Graphics.FromImage(b); // 插值算法的质量 g.InterpolationMode = InterpolationMode.Default; g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch (Exception) { return null; } } 2.按照指定像素大小截图 复制代码 代码如下: View Code /// /// 按照指定大小缩放图片 /// /// /// /// /// public static Bitmap SizeImage(Image srcImage, int iWidth, int iHeight) { try { // 要保存到的图片 Bitmap b = new Bitmap(iWidth, iHeight); Graphics g = Graphics.FromImage(b); // 插值算法的质量 g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(srcImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch (Exception) { return null; } } 3.按照指定像素大小截图(但为了保证图片的原始比例,将对图片从中心进行截取,达到图片不被拉伸的效果) 复制代码 代码如下: View Code /// /// 按照指定大小缩放图片,但是为了保证图片宽高比自动截取 /// /// /// /// /// public static Bitmap SizeImageWithOldPercent(Image srcImage, int iWidth, int iHeight) { try { // 要截取图片的宽度(临时图片) int newW = srcImage.Width; // 要截取图片的高度(临时图片) int newH = srcImage.Height; // 截取开始横坐标(临时图片) int newX = 0; // 截取开始纵坐标(临时图片) int newY = 0; // 截取比例(临时图片) double whPercent = 1; whPercent = ((double)iWidth / (double)iHeight) * ((double)srcImage.Height / (double)srcImage.Width); if (whPercent > 1) { // 当前图片宽度对于要截取比例过大时 newW = int.Parse(Math.Round(srcImage.Width / whPercent).ToString()); } else if (whPercent < 1) { // 当前图片高度对于要截取比例过大时 newH = int.Parse(Math.Round(srcImage.Height * whPercent).ToString()); } if (newW != srcImage.Width) { // 宽度有变化时,调整开始截取的横坐标 newX = Math.Abs(int.Parse(Math.Round(((double)srcImage.Width - newW) / 2).ToString())); } else if (newH == srcImage.Height) { // 高度有变化时,调整开始截取的纵坐标 newY = Math.Abs(int.Parse(Math.Round(((double)srcImage.Height - (double)newH) / 2).ToString())); } // 取得符合比例的临时文件 Bitmap cutedImage = CutImage(srcImage, newX, newY, newW, newH); // 保存到的文件 Bitmap b = new Bitmap(iWidth, iHeight); Graphics g = Graphics.FromImage(b); // 插值算法的质量 g.InterpolationMode = InterpolationMode.Default; g.DrawImage(cutedImage, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(0, 0, cutedImage.Width, cutedImage.Height), GraphicsUnit.Pixel); g.Dispose(); return b; } catch (Exception) { return null; } } 4.jpeg图片质量压缩,压缩的比例参数在1-100之间。(适量的压缩对于肉眼来说没有什么明显的区别,但是能够大大的减小图片的占用大小) 复制代码 代码如下: View Code /// /// jpeg图片压缩 /// /// /// /// /// public static bool GetPicThumbnail(string sFile, string outPath, int flag) { System.Drawing.Image iSource = System.Drawing.Image.FromFile(sFile); ImageFormat tFormat = iSource.RawFormat; //以下代码为保存图片时,设置压缩质量 EncoderParameters ep = new EncoderParameters(); long[] qy = new long[1]; qy[0] = flag;//设置压缩的比例1-100 EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy); ep.Param[0] = eParam; try { ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo jpegICIinfo = null; for (int x = 0; x < arrayICI.Length; x++) { if (arrayICI[x].FormatDescription.Equals("JPEG")) { jpegICIinfo = arrayICI[x]; break; } } if (jpegICIinfo != null) { iSource.Save(outPath, jpegICIinfo, ep);//dFile是压缩后的新路径 } else { iSource.Save(outPath, tFormat); } return true; } catch { return false; } finally { iSource.Dispose(); iSource.Dispose(); } } PS:之上用的CutImage方法的补充 复制代码 代码如下: View Code /// /// 剪裁 -- 用GDI+ /// /// 原始Bitmap /// 开始坐标X /// 开始坐标Y /// 宽度 /// 高度 /// 剪裁后的Bitmap public static Bitmap CutImage(Image b, int StartX, int StartY, int iWidth, int iHeight) { if (b == null) { return null; } int w = b.Width; int h = b.Height; if (StartX >= w || StartY >= h) { // 开始截取坐标过大时,结束处理 return null; } if (StartX + iWidth > w) { // 宽度过大时只截取到最大大小 iWidth = w - StartX; } if (StartY + iHeight > h) { // 高度过大时只截取到最大大小 iHeight = h - StartY; } try { Bitmap bmpOut = new Bitmap(iWidth, iHeight); Graphics g = Graphics.FromImage(bmpOut); g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel); g.Dispose(); return bmpOut; } catch { return null; } } 再次记录下截取的代码,虽然简单,如果重写还是需要花费时间。