1 /// <summary> 2 /// 图片转换 3 /// </summary> 4 public class ImageConvert 5 { 6 public ImageConvert() 7 { } 8 9 public bool ImageConvertToMiniPicture(string originalImagePath, string thumbnailPath, int width, int height, string mode) 10 { 11 try 12 { 13 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 14 15 int iImageWidth = originalImage.Width; 16 int iImageHeight = originalImage.Height; 17 18 originalImage.Dispose(); 19 20 if (iImageWidth > iImageHeight) 21 { 22 this.MakeThumbnail(originalImagePath, thumbnailPath, width, height, "W"); 23 } 24 else 25 { 26 this.MakeThumbnail(originalImagePath, thumbnailPath, width, height, "H"); 27 } 28 29 return true; 30 } 31 catch (Exception) 32 { 33 return false; 34 } 35 } 36 37 public static System.Drawing.Image Crop(System.Drawing.Image originalImage) 38 { 39 int sideLength = 0; 40 if (originalImage.Width > originalImage.Height) 41 sideLength = originalImage.Height; 42 else 43 sideLength = originalImage.Width; 44 45 return Crop(originalImage, -1, -1, sideLength, sideLength); 46 } 47 48 /// <summary> 49 /// 切取给定图像中心位置指定宽度和高度的图像 50 /// </summary> 51 /// <param name="originalImage">原始图像</param> 52 /// <param name="x">水平方向上从该位置开始截取,若未指定则截取水平方向中心位置的图像</param> 53 /// <param name="y">垂直方向上从该位置开始截取,若未指定则截取垂直方向中心位置的图像</param> 54 /// <param name="width">目标图像的宽度</param> 55 /// <param name="height">目标图像的高度</param> 56 /// <returns>目标图像</returns> 57 public static System.Drawing.Image Crop(System.Drawing.Image originalImage, int x, int y, int width, int height) 58 { 59 if (originalImage == null) 60 { 61 throw new ArgumentNullException("originalImage"); 62 } 63 if (width < 0 || height < 0) 64 { 65 throw new ArgumentOutOfRangeException(); 66 } 67 68 69 if (width == 0 && height == 0) 70 { 71 return originalImage.Clone() as System.Drawing.Image; 72 } 73 74 // 如果宽按比例截取... 75 if (width == 0) 76 { 77 width = (int)(height * originalImage.Width / originalImage.Height); 78 } 79 // 如果高按比例截取... 80 if (height == 0) 81 { 82 height = (int)(width * originalImage.Height / originalImage.Width); 83 } 84 85 // 如果希望在水平方向上截取中心位置的图像... 86 if (x < 0) 87 { 88 x = (int)(originalImage.Width - width) / 2; 89 } 90 // 如果希望在垂直方向上截取中心位置的图像... 91 if (y < 0) 92 { 93 y = (int)(originalImage.Height - height) / 2; 94 } 95 96 //新建一个bmp图像 97 System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height); 98 99 //新建一个画板 100 Graphics g = System.Drawing.Graphics.FromImage(bitmap); 101 102 //设置高质量插值法 103 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 104 105 //设置高质量,低速度呈现平滑程度 106 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 107 108 //清空画布并以透明背景色填充 109 g.Clear(Color.Transparent); 110 111 //在指定位置并且按指定大小绘制原图片的指定部分 112 g.DrawImage(originalImage, 113 new Rectangle(0, 0, width, height), 114 new Rectangle(x, y, width, height), 115 GraphicsUnit.Pixel); 116 117 g.Dispose(); 118 119 return bitmap; 120 } 121 122 /// <summary> 123 /// 等比缩放图像,使其短边的最小长度或长边的最大长度等于指定的值。 124 /// </summary> 125 /// <param name="originalImage">原始图像</param> 126 /// <param name="minmaxSideLength">缩放后图像的边长</param> 127 /// <param name="kind">指定边长的类型(短边的最小长度或长边的最大长度)</param> 128 /// <returns></returns> 129 public static System.Drawing.Image Scale(System.Drawing.Image originalImage, int minmaxSideLength, string kind) 130 { 131 int width, height; 132 133 if (kind.ToLower() == "min") 134 { 135 if (originalImage.Width > originalImage.Height) 136 { 137 width = 0; 138 height = minmaxSideLength; 139 } 140 else 141 { 142 width = minmaxSideLength; 143 height = 0; 144 } 145 } 146 else 147 { 148 if (originalImage.Width > originalImage.Height) 149 { 150 width = minmaxSideLength; 151 height = 0; 152 } 153 else 154 { 155 width = 0; 156 height = minmaxSideLength; 157 } 158 } 159 return Scale(originalImage, width, height); 160 } 161 162 /// <summary> 163 /// 缩放图像,使其宽度和高度等于指定的值。若宽度或高度等于0,则图像将仅随不为0的边缩放(即等比缩放),若宽度和高度都等于0,则不进行缩放。 164 /// </summary> 165 /// <param name="originalImage">待缩放的图像</param> 166 /// <param name="width">缩放后图像的宽度。若该参数为0,则图像在水平方向上按比例缩放。</param> 167 /// <param name="height">缩放后图像的高度。若该参数为0,则图像在垂直方向上按比例缩放。</param> 168 /// <returns>缩放后的图像。</returns> 169 public static System.Drawing.Image Scale(System.Drawing.Image originalImage, int width, int height) 170 { 171 if (originalImage == null) 172 { 173 throw new ArgumentNullException("originalImage"); 174 } 175 if (width < 0 || height < 0) 176 { 177 throw new ArgumentOutOfRangeException(); 178 } 179 180 181 if (width == 0 && height == 0) 182 { 183 return originalImage.Clone() as System.Drawing.Image; 184 } 185 186 if (width == 0) 187 { 188 width = (int)(height * originalImage.Width / originalImage.Height); 189 } 190 else if (height == 0) 191 { 192 height = (int)(width * originalImage.Height / originalImage.Width); 193 } 194 195 196 //新建一个bmp图片 197 System.Drawing.Image bitmap = new System.Drawing.Bitmap(width, height); 198 199 //新建一个画板 200 Graphics g = System.Drawing.Graphics.FromImage(bitmap); 201 202 //设置高质量插值法 203 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 204 205 //设置高质量,低速度呈现平滑程度 206 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 207 208 //清空画布并以透明背景色填充 209 g.Clear(Color.Transparent); 210 211 //在指定位置并且按指定大小绘制原图片的指定部分 212 g.DrawImage(originalImage, 213 new Rectangle(0, 0, width, height), 214 new Rectangle(0, 0, originalImage.Width, originalImage.Height), 215 GraphicsUnit.Pixel); 216 217 g.Dispose(); 218 219 return bitmap; 220 } 221 222 /// <summary> 223 /// 保存图像到指定文件 224 /// </summary> 225 /// <param name="image"></param> 226 /// <param name="fileName"></param> 227 public static void Save(System.Drawing.Image image, string filePath, long quality) 228 { 229 //控制图片的质量等级 230 ImageCodecInfo myImageCodecInfo; 231 System.Drawing.Imaging.Encoder myEncoder; 232 EncoderParameter myEncoderParameter; 233 EncoderParameters myEncoderParameters; 234 235 string mimeType = GetImageFormat(filePath); 236 237 myImageCodecInfo = GetEncoderInfo(mimeType); 238 myEncoder = System.Drawing.Imaging.Encoder.Quality; 239 myEncoderParameters = new EncoderParameters(1); 240 myEncoderParameter = new EncoderParameter(myEncoder, quality); 241 myEncoderParameters.Param[0] = myEncoderParameter; 242 243 try 244 { 245 System.IO.FileInfo tempFile = new System.IO.FileInfo(filePath); 246 if (!tempFile.Directory.Exists) 247 { 248 System.IO.Directory.CreateDirectory(tempFile.Directory.ToString()); 249 } 250 image.Save(filePath, myImageCodecInfo, myEncoderParameters); 251 } 252 catch (System.Exception e) 253 { 254 image.Dispose(); 255 throw e; 256 } 257 finally 258 { 259 image.Dispose(); 260 } 261 } 262 263 /// <summary> 264 /// 获取图片保存格式 265 /// </summary> 266 /// <param name="filePath"></param> 267 /// <returns></returns> 268 private static string GetImageFormat(string filePath) 269 { 270 string suffix = GetFileExtension(filePath).ToUpper(); 271 string mimeType = String.Empty; 272 273 switch (suffix) 274 { 275 case "JPG": 276 mimeType = "image/jpeg"; 277 break; 278 case "PNG": 279 mimeType = "image/png"; 280 break; 281 default: 282 mimeType = "image/jpeg"; 283 break; 284 285 } 286 287 return mimeType; 288 } 289 290 /// <summary> 291 /// 获取ImageCodecInfo 292 /// </summary> 293 /// <param name="mimeType"></param> 294 /// <returns></returns> 295 private static ImageCodecInfo GetEncoderInfo(string mimeType) 296 { 297 ImageCodecInfo[] encoders; 298 encoders = ImageCodecInfo.GetImageEncoders(); 299 300 for (int i = 0; i < encoders.Length; i++) 301 { 302 if (encoders[i].MimeType.Equals(mimeType)) 303 { 304 return encoders[i]; 305 } 306 } 307 308 return null; 309 } 310 311 312 /// <summary> 313 /// 获取文件扩展名 314 /// </summary> 315 /// <param name="fileName"></param> 316 /// <returns></returns> 317 public static string GetFileExtension(string fileName) 318 { 319 int startIndex = fileName.LastIndexOf('.'); 320 return fileName.Substring(startIndex + 1, fileName.Length - 1 - startIndex); 321 } 322 323 /**/ 324 /// <summary> 325 /// 生成缩略图 326 /// </summary> 327 /// <param name="originalImagePath">源图路径(物理路径)</param> 328 /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 329 /// <param name="width">缩略图宽度</param> 330 /// <param name="height">缩略图高度</param> 331 /// <param name="mode">生成缩略图的方式</param> 332 private void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) 333 { 334 int towidth = width; 335 int toheight = height; 336 337 int x = 0; 338 int y = 0; 339 int ow = 0; 340 int oh = 0; 341 342 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 343 ow = originalImage.Width; 344 oh = originalImage.Height; 345 //originalImage.Dispose(); 346 347 switch (mode) 348 { 349 case "HW"://指定高宽缩放(可能变形) 350 break; 351 case "W"://指定宽,高按比例 352 toheight = oh * width / ow; 353 break; 354 case "H"://指定高,宽按比例 355 towidth = ow * height / oh; 356 break; 357 default: 358 break; 359 } 360 361 //新建一个bmp图片 362 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 363 364 //新建一个画板 365 Graphics g = System.Drawing.Graphics.FromImage(bitmap); 366 367 //设置高质量插值法 368 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 369 370 //设置高质量,低速度呈现平滑程度 371 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 372 373 //清空画布并以透明背景色填充 374 g.Clear(Color.Transparent); 375 376 //获得要画图片的信息//*这里很重要*// 377 ImageCodecInfo myImageCodecInfo; 378 System.Drawing.Imaging.Encoder myEncoder; 379 EncoderParameter myEncoderParameter; 380 EncoderParameters myEncoderParameters; 381 myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[1]; 382 myEncoder = System.Drawing.Imaging.Encoder.Quality; 383 myEncoderParameters = new EncoderParameters(1); 384 myEncoderParameter = new EncoderParameter(myEncoder, 100L); 385 myEncoderParameters.Param[0] = myEncoderParameter; 386 //***********************************************// 387 388 //在指定位置并且按指定大小绘制原图片的指定部分 389 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), 390 new Rectangle(x, y, ow, oh), 391 GraphicsUnit.Pixel); 392 393 try 394 { 395 //以jpg格式保存缩略图 396 //bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 397 bitmap.Save(thumbnailPath, myImageCodecInfo, myEncoderParameters); 398 originalImage.Dispose(); 399 bitmap.Dispose(); 400 g.Dispose(); 401 } 402 catch (System.Exception e) 403 { 404 originalImage.Dispose(); 405 bitmap.Dispose(); 406 g.Dispose(); 407 throw e; 408 } 409 finally 410 { 411 originalImage.Dispose(); 412 bitmap.Dispose(); 413 g.Dispose(); 414 } 415 } 416 /**/ 417 /// <summary> 418 /// 生成缩略图 419 /// </summary> 420 /// <param name="originalImagePath">源图路径(物理路径)</param> 421 /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 422 /// <param name="width">缩略图宽度</param> 423 /// <param name="height">缩略图高度</param> 424 /// <param name="mode">生成缩略图的方式</param> 425 private void MakeOperateImage(string originalImagePath, string thumbnailPath, int width, int height, string mode) 426 { 427 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 428 429 430 int towidth = width; 431 int toheight = height; 432 433 int x = 0; 434 int y = 0; 435 int ow = originalImage.Width; 436 int oh = originalImage.Height; 437 438 switch (mode) 439 { 440 case "HW"://指定高宽缩放(可能变形) 441 break; 442 case "W"://指定宽,高按比例 443 toheight = originalImage.Height * width / originalImage.Width; 444 break; 445 case "H"://指定高,宽按比例 446 towidth = originalImage.Width * height / originalImage.Height; 447 break; 448 case "Cut"://指定高宽裁减(不变形) 449 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) 450 { 451 oh = originalImage.Height; 452 ow = originalImage.Height * towidth / toheight; 453 y = 0; 454 x = (originalImage.Width - ow) / 2; 455 } 456 else 457 { 458 ow = originalImage.Width; 459 oh = originalImage.Width * height / towidth; 460 x = 0; 461 y = (originalImage.Height - oh) / 2; 462 } 463 break; 464 default: 465 break; 466 } 467 468 //新建一个bmp图片 469 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 470 471 //新建一个画板 472 Graphics g = System.Drawing.Graphics.FromImage(bitmap); 473 474 //设置高质量插值法 475 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 476 477 //设置高质量,低速度呈现平滑程度 478 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 479 480 //清空画布并以透明背景色填充 481 g.Clear(Color.Transparent); 482 483 //获得要画图片的信息//*这里很重要*// 484 ImageCodecInfo myImageCodecInfo; 485 System.Drawing.Imaging.Encoder myEncoder; 486 EncoderParameter myEncoderParameter; 487 EncoderParameters myEncoderParameters; 488 myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[1]; 489 myEncoder = System.Drawing.Imaging.Encoder.Quality; 490 myEncoderParameters = new EncoderParameters(1); 491 myEncoderParameter = new EncoderParameter(myEncoder, 100L); 492 myEncoderParameters.Param[0] = myEncoderParameter; 493 //***********************************************// 494 495 //在指定位置并且按指定大小绘制原图片的指定部分 496 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), 497 new Rectangle(x, y, ow, oh), 498 GraphicsUnit.Pixel); 499 500 try 501 { 502 //以jpg格式保存缩略图 503 //bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 504 bitmap.Save(thumbnailPath, myImageCodecInfo, myEncoderParameters); 505 originalImage.Dispose(); 506 bitmap.Dispose(); 507 g.Dispose(); 508 } 509 catch (System.Exception e) 510 { 511 originalImage.Dispose(); 512 bitmap.Dispose(); 513 g.Dispose(); 514 throw e; 515 } 516 finally 517 { 518 originalImage.Dispose(); 519 bitmap.Dispose(); 520 g.Dispose(); 521 } 522 } 523 }