PageRenderTime 14ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/eBlog/eBlog.Common/Image/ImageHelper.cs

#
C# | 358 lines | 279 code | 41 blank | 38 comment | 25 complexity | a5fb1d6199475e652b0e88b2116782f0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing.Imaging;
  4. using System.Text;
  5. using System.Drawing;
  6. namespace eBlog.Common
  7. {
  8. public class ImageHelper
  9. {
  10. /// <summary>
  11. /// 生成缩略图
  12. /// </summary>
  13. /// <param name="originalImagePath">源图路径(物理路径)</param>
  14. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  15. /// <param name="width">缩略图宽度</param>
  16. /// <param name="height">缩略图高度</param>
  17. /// <param name="mode">生成缩略图的方式</param>
  18. public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  19. {
  20. Image originalImage = Image.FromFile(originalImagePath);
  21. int towidth = width;
  22. int toheight = height;
  23. int x = 0;
  24. int y = 0;
  25. int ow = originalImage.Width;
  26. int oh = originalImage.Height;
  27. switch (mode)
  28. {
  29. case "HW"://指定高宽缩放(可能变形)
  30. break;
  31. case "W"://指定宽,高按比例
  32. toheight = originalImage.Height * width / originalImage.Width;
  33. break;
  34. case "H"://指定高,宽按比例
  35. towidth = originalImage.Width * height / originalImage.Height;
  36. break;
  37. case "Cut"://指定高宽裁减(不变形)
  38. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  39. {
  40. oh = originalImage.Height;
  41. ow = originalImage.Height * towidth / toheight;
  42. y = 0;
  43. x = (originalImage.Width - ow) / 2;
  44. }
  45. else
  46. {
  47. ow = originalImage.Width;
  48. oh = originalImage.Width * height / towidth;
  49. x = 0;
  50. y = (originalImage.Height - oh) / 2;
  51. }
  52. break;
  53. default:
  54. break;
  55. }
  56. //新建一个bmp图片
  57. Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  58. //新建一个画板
  59. Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  60. //设置高质量插值法
  61. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  62. //设置高质量,低速度呈现平滑程度
  63. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  64. //清空画布并以透明背景色填充
  65. g.Clear(Color.Transparent);
  66. //在指定位置并且按指定大小绘制原图片的指定部分
  67. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  68. new Rectangle(x, y, ow, oh),
  69. GraphicsUnit.Pixel);
  70. try
  71. {
  72. //以jpg格式保存缩略图
  73. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  74. }
  75. catch (System.Exception e)
  76. {
  77. throw e;
  78. }
  79. finally
  80. {
  81. originalImage.Dispose();
  82. bitmap.Dispose();
  83. g.Dispose();
  84. }
  85. }
  86. /// <summary>
  87. /// 添加图片水印
  88. /// </summary>
  89. /// <param name="oldFilePath">原始图片路径</param>
  90. /// <param name="newFilePath">将要添加水印图片路径</param>
  91. /// <param name="waterPosition">水印位置</param>
  92. /// <param name="waterImagePath">水印图片路径</param>
  93. /// <param name="watermarkTransparency"></param>
  94. /// <param name="quality">质量</param>
  95. public static void CreateWaterImage(string oldFilePath, string newFilePath, int waterPosition, string waterImagePath, int watermarkTransparency, int quality)
  96. {
  97. System.Drawing.Image image = System.Drawing.Image.FromFile(oldFilePath);
  98. Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
  99. Graphics g = Graphics.FromImage(bmp);
  100. g.Clear(Color.White);
  101. g.DrawImage(image, 0, 0, image.Width, image.Height);
  102. //设置透明度
  103. System.Drawing.Image watermark = new Bitmap(waterImagePath);
  104. ImageAttributes imageAttributes = new ImageAttributes();
  105. ColorMap colorMap = new ColorMap();
  106. colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
  107. colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
  108. ColorMap[] remapTable = { colorMap };
  109. imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
  110. float transparency = 0.5F;
  111. if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
  112. {
  113. transparency = (watermarkTransparency / 10.0F);
  114. }
  115. float[][] colorMatrixElements = {
  116. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  117. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  118. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  119. new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f}, //注意:倒数第二处为0.0f为完全透明,1.0f为完全不透明
  120. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  121. };
  122. ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
  123. imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  124. int _width = image.Width;
  125. int _height = image.Height;
  126. int xpos = 0;
  127. int ypos = 0;
  128. int WatermarkWidth = 0;
  129. int WatermarkHeight = 0;
  130. double bl = 1d;
  131. //计算水印图片的比率
  132. //取背景的1/4宽度来比较
  133. if ((_width > watermark.Width * 2) && (_height > watermark.Height * 2))
  134. {
  135. bl = 1;
  136. }
  137. else if ((_width > watermark.Width * 2) && (_height < watermark.Height * 2))
  138. {
  139. bl = Convert.ToDouble(_height / 2) / Convert.ToDouble(watermark.Height);
  140. }
  141. else if ((_width < watermark.Width * 2) && (_height > watermark.Height * 2))
  142. {
  143. bl = Convert.ToDouble(_width / 2) / Convert.ToDouble(watermark.Width);
  144. }
  145. else
  146. {
  147. if ((_width * watermark.Height) > (_height * watermark.Width))
  148. {
  149. bl = Convert.ToDouble(_height / 2) / Convert.ToDouble(watermark.Height);
  150. }
  151. else
  152. {
  153. bl = Convert.ToDouble(_width / 2) / Convert.ToDouble(watermark.Width);
  154. }
  155. }
  156. WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
  157. WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
  158. switch (waterPosition)
  159. {
  160. case 3:
  161. xpos = _width - WatermarkWidth - 10;
  162. ypos = 10;
  163. break;
  164. case 2:
  165. xpos = 10;
  166. ypos = _height - WatermarkHeight - 10;
  167. break;
  168. case 5:
  169. xpos = _width / 2 - WatermarkWidth / 2;
  170. ypos = _height / 2 - WatermarkHeight / 2;
  171. break;
  172. case 1:
  173. xpos = 10;
  174. ypos = 10;
  175. break;
  176. case 4:
  177. default:
  178. xpos = _width - WatermarkWidth - 10;
  179. ypos = _height - WatermarkHeight - 10;
  180. break;
  181. }
  182. g.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
  183. try
  184. {
  185. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  186. ImageCodecInfo ici = null;
  187. foreach (ImageCodecInfo codec in codecs)
  188. {
  189. if (codec.MimeType.IndexOf("jpeg") > -1)
  190. {
  191. ici = codec;
  192. }
  193. }
  194. EncoderParameters encoderParams = new EncoderParameters();
  195. long[] qualityParam = new long[1];
  196. if (quality < 0 || quality > 100)
  197. {
  198. quality = 80;
  199. }
  200. qualityParam[0] = quality;
  201. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
  202. encoderParams.Param[0] = encoderParam;
  203. if (ici != null)
  204. {
  205. bmp.Save(newFilePath, ici, encoderParams);
  206. }
  207. else
  208. {
  209. bmp.Save(newFilePath);
  210. }
  211. }
  212. catch (Exception ex)
  213. {
  214. throw ex;
  215. }
  216. finally
  217. {
  218. watermark.Dispose();
  219. imageAttributes.Dispose();
  220. image.Dispose();
  221. bmp.Dispose();
  222. }
  223. }
  224. /// <summary>
  225. /// 添加文字水印
  226. /// </summary>
  227. /// <param name="oldFilePath">原始图片路径</param>
  228. /// <param name="newFilePath">将要添加水印图片路径</param>
  229. /// <param name="waterPosition">水印位置</param>
  230. /// <param name="waterText">水印内容</param>
  231. /// <param name="quality"></param>
  232. /// <param name="fontname"></param>
  233. /// <param name="fontsize"></param>
  234. public static void CreateWaterText(string oldFilePath, string newFilePath, int waterPosition, string waterText, int quality, string fontname, int fontsize)
  235. {
  236. System.Drawing.Image image = System.Drawing.Image.FromFile(oldFilePath);
  237. Bitmap bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppArgb);
  238. Graphics g = Graphics.FromImage(bmp);
  239. g.Clear(Color.White);
  240. g.DrawImage(image, 0, 0, image.Width, image.Height);
  241. int _width = bmp.Width;
  242. int _height = bmp.Height;
  243. Font crFont = new Font(fontname, fontsize, FontStyle.Bold, GraphicsUnit.Pixel);
  244. SizeF crSize = g.MeasureString(waterText, crFont);
  245. float xpos = 0;
  246. float ypos = 0;
  247. switch (waterPosition)
  248. {
  249. case 3:
  250. xpos = ((float)_width * (float).99) - (crSize.Width / 2);
  251. ypos = (float)_height * (float).01;
  252. break;
  253. case 2:
  254. xpos = ((float)_width * (float).01) + (crSize.Width / 2);
  255. ypos = ((float)_height * (float).99) - crSize.Height;
  256. break;
  257. case 5:
  258. xpos = ((_width - crSize.Width) / 2) + crSize.Width / 2; //奇怪的表达式
  259. ypos = (_height - crSize.Height) / 2 + crSize.Height / 2;
  260. break;
  261. case 1:
  262. xpos = ((float)_width * (float).01) + (crSize.Width / 2);
  263. ypos = (float)_height * (float).01;
  264. break;
  265. case 4:
  266. default:
  267. xpos = ((float)_width * (float).99) - (crSize.Width / 2);
  268. ypos = ((float)_height * (float).99) - crSize.Height;
  269. break;
  270. }
  271. StringFormat StrFormat = new StringFormat();
  272. StrFormat.Alignment = StringAlignment.Center;
  273. //可设置透明度
  274. SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(255, 255, 255, 255));
  275. g.DrawString(waterText, crFont, semiTransBrush, xpos, ypos, StrFormat);
  276. try
  277. {
  278. ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
  279. ImageCodecInfo ici = null;
  280. foreach (ImageCodecInfo codec in codecs)
  281. {
  282. if (codec.MimeType.IndexOf("jpeg") > -1)
  283. {
  284. ici = codec;
  285. }
  286. }
  287. EncoderParameters encoderParams = new EncoderParameters();
  288. long[] qualityParam = new long[1];
  289. if (quality < 0 || quality > 100)
  290. {
  291. quality = 80;
  292. }
  293. qualityParam[0] = quality;
  294. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
  295. encoderParams.Param[0] = encoderParam;
  296. if (ici != null)
  297. {
  298. bmp.Save(newFilePath, ici, encoderParams);
  299. }
  300. else
  301. {
  302. bmp.Save(newFilePath);
  303. }
  304. }
  305. catch (Exception ex)
  306. {
  307. throw ex;
  308. }
  309. finally
  310. {
  311. semiTransBrush.Dispose();
  312. image.Dispose();
  313. bmp.Dispose();
  314. }
  315. }
  316. }
  317. }