标签:
压缩不变形的代码
/// <summary> /// 表示对图片作裁剪操作的类。 /// </summary> public class CropHelper { /// <summary> /// 表示对图片进行不拉伸的比例裁剪。 /// </summary> /// <param name="sourceFileName">源文件地址。</param> /// <param name="thumbnaiFileName">缩略图文件地址。</param> /// <param name="thumbnailWidth">缩略图宽度。</param> /// <param name="thumbnailHeight">缩略图高度。</param> public static void Thumb(string sourceFileName, string thumbnaiFileName, int thumbnailWidth, int thumbnailHeight) { var sourceImage = Image.FromFile(sourceFileName); //Thumb(originalImage, thumbnaiFile, thumbnailWidth, thumbnailHeight); var originalWidth = sourceImage.Width; var originalHeight = sourceImage.Height; //压缩图片宽高尺寸超出或者等于原图尺寸大小,返回原图 if (thumbnailWidth >= originalWidth && thumbnailHeight >= originalHeight) { File.Copy(sourceFileName, thumbnaiFileName, true); return; } var destRect = new Rectangle(); var srcRect = new Rectangle(); //裁剪长度和宽度都小于原始图片尺寸,比例大的一边直接压缩,小的一边根据大的比例压缩后再裁剪 if (thumbnailWidth < originalWidth && thumbnailHeight < originalHeight) { destRect.Width = thumbnailWidth; destRect.Height = thumbnailHeight; //等比例裁剪 var widthRatio = (double)thumbnailWidth / originalWidth; var heightRatio = (double)thumbnailHeight / originalHeight; if (widthRatio > heightRatio) { srcRect.Width = originalWidth; srcRect.Height = (int)(thumbnailHeight / widthRatio); srcRect.Y = (int)(originalHeight - srcRect.Height) / 2; } else if (widthRatio < heightRatio) { srcRect.Width = (int)(thumbnailWidth / heightRatio); srcRect.Height = originalHeight; srcRect.X = (int)(originalWidth - srcRect.Width) / 2; } else { srcRect.Width = originalWidth; srcRect.Height = originalHeight; } } if (thumbnailWidth >= originalWidth && thumbnailHeight < originalHeight) { //原图宽度,裁剪高度 destRect.Width = originalWidth; destRect.Height = thumbnailHeight; //截取图片位置 srcRect.Y = (originalHeight - thumbnailHeight) / 2; srcRect.Width = originalWidth; srcRect.Height = thumbnailHeight; } if (thumbnailWidth < originalWidth && thumbnailHeight >= originalHeight) { //裁剪宽度,原图高度 destRect.Width = thumbnailWidth; destRect.Height = originalHeight; //截取图片位置 srcRect.X = (originalWidth - thumbnailWidth) / 2; srcRect.Width = thumbnailWidth; srcRect.Height = originalHeight; } using (var bitmap = new Bitmap(destRect.Width, destRect.Height)) { using (var g = Graphics.FromImage(bitmap)) { g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawImage(sourceImage, destRect, srcRect, GraphicsUnit.Pixel); } bitmap.Save(thumbnaiFileName); } } }
标签:
原文地址:http://www.cnblogs.com/linkjr/p/image-compress.html