完善一下别人的方法,成自己好用的工具
1 using System.Drawing; 2 using System.Drawing.Imaging; 3 4 namespace GXNUQzzx.Tools.Utility 5 { 6 /// <summary> 7 /// 图片缩放模式 8 /// </summary> 9 public enum ScaleMode 10 { 11 /// <summary> 12 /// 指定高宽缩放(可能失真) 13 /// </summary> 14 Normal, 15 16 /// <summary> 17 /// 指定高,宽按比例 18 /// </summary> 19 Width, 20 21 /// <summary> 22 /// 指定宽,高按比例 23 /// </summary> 24 Height, 25 26 /// <summary> 27 /// 指定高宽裁减(不失真) 28 /// </summary> 29 Cut 30 } 31 32 /// <summary> 33 /// 图片辅助类 34 /// </summary> 35 public static class ImageHelper 36 { 37 /// <summary> 38 /// 生成缩略图 39 /// </summary> 40 /// <param name="originalImagePath">源图路径(物理路径)</param> 41 /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 42 /// <param name="width">缩略图宽度</param> 43 /// <param name="height">缩略图高度</param> 44 /// <param name="mode">生成缩略图的方式</param> 45 public static void GenerateThumbnail(string originalImagePath, string thumbnailPath, int width, int height, ScaleMode mode) 46 { 47 using (Image originalImage = Image.FromFile(originalImagePath)) 48 { 49 GenerateThumbnail(originalImage, thumbnailPath, width, height, mode); 50 } 51 } 52 53 /// <summary> 54 /// 生成缩略图 55 /// </summary> 56 /// <param name="originalImage">源图</param> 57 /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 58 /// <param name="width">缩略图宽度</param> 59 /// <param name="height">缩略图高度</param> 60 /// <param name="mode">生成缩略图的方式</param> 61 public static void GenerateThumbnail(Image originalImage, string thumbnailPath, int width, int height, ScaleMode mode) 62 { 63 using (Image image = GenerateThumbnail(originalImage, width, height, mode)) 64 { 65 image.Save(thumbnailPath, ImageFormat.Jpeg); 66 } 67 } 68 69 /// <summary> 70 /// 生成缩略图 71 /// </summary> 72 /// <param name="originalImage">源图</param> 73 /// <param name="width">缩略图宽度</param> 74 /// <param name="height">缩略图高度</param> 75 /// <param name="mode">生成缩略图的方式</param> 76 /// <returns>生成的缩略图</returns> 77 public static Image GenerateThumbnail(Image originalImage, int width, int height, ScaleMode mode) 78 { 79 int towidth = width; 80 int toheight = height; 81 82 int x = 0; 83 int y = 0; 84 int ow = originalImage.Width; 85 int oh = originalImage.Height; 86 87 #region // 计算缩略图宽高 88 switch (mode) 89 { 90 default: 91 case ScaleMode.Normal: { break; } 92 case ScaleMode.Height: 93 { 94 toheight = originalImage.Height * width / originalImage.Width; 95 break; 96 } 97 case ScaleMode.Width: 98 { 99 towidth = originalImage.Width * height / originalImage.Height; 100 break; 101 } 102 case ScaleMode.Cut: 103 { 104 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) 105 { 106 oh = originalImage.Height; 107 ow = originalImage.Height * towidth / toheight; 108 y = 0; 109 x = (originalImage.Width - ow) / 2; 110 } 111 else 112 { 113 ow = originalImage.Width; 114 oh = originalImage.Width * height / towidth; 115 x = 0; 116 y = (originalImage.Height - oh) / 2; 117 } 118 break; 119 } 120 } 121 #endregion 122 123 //新建一个bmp图片 124 Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 125 126 //新建一个画板 127 using (Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 128 { 129 //设置高质量插值法 130 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 131 132 //设置高质量,低速度呈现平滑程度 133 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 134 135 //清空画布并以透明背景色填充 136 g.Clear(Color.Transparent); 137 138 //在指定位置并且按指定大小绘制原图片的指定部分 139 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel); 140 } 141 142 // 返回生成的缩略图 143 return bitmap; 144 } 145 146 /// <summary> 147 /// 按原像素比例生成一张新图片 148 /// </summary> 149 /// <param name="oldImagePath">源图片路径(物理路径)</param> 150 /// <param name="newImagePath">新图片路径(物理路径)</param> 151 public static void GenerateNewImage(string oldImagePath, string newImagePath) 152 { 153 using (Image originalImage = Image.FromFile(oldImagePath)) 154 { 155 GenerateNewImage(originalImage, newImagePath); 156 } 157 } 158 159 /// <summary> 160 /// 按原像素比例生成一张新图片 161 /// </summary> 162 /// <param name="oldImage">源图片</param> 163 /// <param name="newImagePath">新图片路径(物理路径)</param> 164 public static void GenerateNewImage(Image oldImage, string newImagePath) 165 { 166 using (Image image = GenerateNewImage(oldImage)) 167 { 168 image.Save(newImagePath, ImageFormat.Jpeg); 169 } 170 } 171 172 /// <summary> 173 /// 按原像素比例生成一张新图片 174 /// </summary> 175 /// <param name="oldImage">源图片</param> 176 /// <returns>新图片</returns> 177 public static Image GenerateNewImage(Image oldImage) 178 { 179 int width = oldImage.Width; 180 int height = oldImage.Height; 181 182 //新建一个bmp图片 183 Image bitmap = new System.Drawing.Bitmap(width, height); 184 185 //新建一个画板 186 using (Graphics g = System.Drawing.Graphics.FromImage(bitmap)) 187 { 188 //设置高质量插值法 189 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 190 191 //设置高质量,低速度呈现平滑程度 192 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 193 194 //清空画布并以透明背景色填充 195 g.Clear(Color.Transparent); 196 197 //创建矩形 198 var rectangle = new Rectangle(0, 0, width, height); 199 200 //在指定位置并且按指定大小绘制原图片的指定部分 201 g.DrawImage(oldImage, rectangle, rectangle, GraphicsUnit.Pixel); 202 } 203 204 // 返回新图片 205 return bitmap; 206 } 207 } 208 }