剪裁图片.cs
//imgSrc 原图片路径 //savePath 保存剪裁后的图片路径 private void MakeThumbnailImage(string imgSrc, string savePath, string fileSuffix, int width, int height, string model) { //load img Image original = Image.FromFile(imgSrc); int towidth = width; int toheight = height; int x = 0; int y = 0; int imageWidth = original.Width; int imageHeight = original.Height; switch (model) { case "HW": //指定宽高缩放(补白) if (((double)original.Width / (double)original.Height) > ((double)towidth / (double)toheight)) { imageWidth = original.Width; imageHeight = original.Width * height / towidth; x = 0; y = (original.Height - imageHeight) / 2; } else { imageHeight = original.Height; imageWidth = original.Height * towidth / toheight; y = 0; x = (original.Width - imageWidth) / 2; } break; case "W"://指定宽高比例 toheight = original.Height * width / original.Width; break; case "H"://指定高宽比例 towidth = original.Width * height / original.Height; break; case "Cut"://指定高宽裁剪(不变形) if (((double)original.Width / (double)original.Height) > ((double)towidth / (double)toheight)) { imageHeight = original.Height; imageWidth = original.Height * towidth / toheight; y = 0; x = (original.Width - imageWidth) / 2; } else { imageWidth = original.Width; imageHeight = original.Width * height / towidth; x = 0; y = (original.Height - imageHeight) / 2; } break; default: break; } //创建图片 Bitmap b = new Bitmap(towidth, toheight); using (Graphics g = Graphics.FromImage(b)) //在图片上创建画板 { g.InterpolationMode = InterpolationMode.HighQualityBicubic; //高质量插值算法 g.SmoothingMode = SmoothingMode.AntiAlias; //抗锯齿 g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.Clear(Color.White); //清空画板 白色填充 g.DrawImage(original, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, imageWidth, imageHeight), GraphicsUnit.Pixel); //save SaveImage(b, savePath, GetCodecInfo("image/" + GetFormat(fileSuffix).ToString().ToLower())); } b.Dispose(); //释放资源 } private void SaveImage(Image img, string savePath, ImageCodecInfo ici) { //设置 原图 对象的 EncoderParameters using (EncoderParameters parameters = new EncoderParameters()) { parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)100)); img.Save(savePath, ici, parameters); } } private ImageCodecInfo GetCodecInfo(string mimeType) { ImageCodecInfo[] codecInfo = ImageCodecInfo.GetImageEncoders(); foreach (var item in codecInfo) { if (item.MimeType == mimeType) return item; } return null; } private ImageFormat GetFormat(string fileSuffix) { switch (fileSuffix.ToLower()) { case "jpg": case "jpeg": return ImageFormat.Jpeg; case "bmp": return ImageFormat.Bmp; case "png": return ImageFormat.Png; case "gif": return ImageFormat.Gif; case "emf": return ImageFormat.Emf; case "ico": return ImageFormat.Icon; case "exif": return ImageFormat.Exif; case "tiff": return ImageFormat.Tiff; case "wmf": return ImageFormat.Wmf; default: return ImageFormat.Jpeg; } }
相关推荐
-
清除某个文件夹下某种类型的文件 csharp
2019-1-7
-
动态生产有规律的ID csharp
2019-1-7
-
串口数据读取(网络摘抄,出处记不清了)可降低cpu占用 csharp
2019-1-8
-
动态生成PropertyGrid加载的类 csharp
2019-1-7
-
Excel操作帮助类 csharp
2019-1-8
-
.net core webapi+vue实现登录图片验证码功能 csharp
2019-1-8
-
C#合并图片、图片等比缩放 csharp
2019-1-7
-
c#的DateTime.Now函数详解 csharp
2019-1-7
-
C#随机数 csharp
2019-1-7
-
阿拉伯数字金额转繁体金额算法 csharp
2019-1-7