当前位置:   article > 正文

.net上传图片的处理类库(缩略图、水印)_.net6 图片处理库

.net6 图片处理库
  1. public static class ImageHelp
  2. {
  3. #region 生成缩略图
  4. /// <summary>
  5. /// 生成缩略图
  6. /// </summary>
  7. /// <param name="originalImagePath">源图路径(物理路径)</param>
  8. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  9. /// <param name="width">缩略图宽度</param>
  10. /// <param name="height">缩略图高度</param>
  11. /// <param name="mode">生成缩略图的方式
  12. /// <code>HW:指定高宽缩放(可能变形)</code>
  13. /// <code>W:指定宽,高按比例 </code>
  14. /// <code>H:指定高,宽按比例</code>
  15. /// <code>CUT:指定高宽裁减(不变形) </code>
  16. /// <code>FILL:填充</code>
  17. /// </param>
  18. public static bool LocalImage2Thumbs(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  19. {
  20. System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  21. Image2Thumbs(originalImage, thumbnailPath, width, height, mode);
  22. originalImage.Dispose();
  23. return true;
  24. }
  25. /// <summary>
  26. /// 生成远程图片的缩略图
  27. /// </summary>
  28. /// <param name="remoteImageUrl"></param>
  29. /// <param name="thumbnailPath"></param>
  30. /// <param name="width"></param>
  31. /// <param name="height"></param>
  32. /// <param name="mode"></param>
  33. /// <returns></returns>
  34. public static bool RemoteImage2Thumbs(string remoteImageUrl, string thumbnailPath, int width, int height, string mode)
  35. {
  36. try
  37. {
  38. WebRequest request = WebRequest.Create(remoteImageUrl);
  39. request.Timeout = 20000;
  40. Stream stream = request.GetResponse().GetResponseStream();
  41. System.Drawing.Image originalImage = System.Drawing.Image.FromStream(stream);
  42. Image2Thumbs(originalImage, thumbnailPath, width, height, mode);
  43. originalImage.Dispose();
  44. return true;
  45. }
  46. catch
  47. {
  48. return false;
  49. }
  50. }
  51. /// <summary>
  52. /// 生成缩略图
  53. /// </summary>
  54. /// <param name="originalImage">源图</param>
  55. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  56. /// <param name="photoWidth">最终缩略图宽度</param>
  57. /// <param name="height">最终缩略图高度</param>
  58. /// <param name="mode">生成缩略图的方式
  59. /// <code>HW:指定高宽缩放(可能变形)</code>
  60. /// <code>W:指定宽,高按比例 </code>
  61. /// <code>H:指定高,宽按比例</code>
  62. /// <code>CUT:指定高宽裁减(不变形) </code>
  63. /// <code>FILL:填充</code>
  64. /// </param>
  65. public static void Image2Thumbs(System.Drawing.Image originalImage, string thumbnailPath, int photoWidth, int photoHeight, string mode)
  66. {
  67. #region 开始画图
  68. int lastPhotoWidth = photoWidth;//最后缩略图的宽度
  69. int lastPhotoHeight = photoHeight;//最后缩略图的高度
  70. int toWidth = photoWidth;//原图片被压缩的宽度
  71. int toHeight = photoHeight;//原图片被压缩的高度
  72. int x = 0;
  73. int y = 0;
  74. int ow = originalImage.Width;
  75. int oh = originalImage.Height;
  76. int bg_x = 0;
  77. int bg_y = 0;
  78. switch (mode.ToUpper())
  79. {
  80. case "FILL"://压缩填充至指定区域
  81. toHeight = photoHeight;
  82. toWidth = toHeight * ow / oh;
  83. if (toWidth > photoWidth)
  84. {
  85. toHeight = toHeight * photoWidth / toWidth;
  86. toWidth = photoWidth;
  87. }
  88. bg_x = (photoWidth - toWidth) / 2;
  89. bg_y = (photoHeight - toHeight) / 2;
  90. break;
  91. case "HW"://指定高宽缩放(可能变形)
  92. break;
  93. case "W"://指定宽,高按比例
  94. toHeight = lastPhotoHeight = originalImage.Height * photoWidth / originalImage.Width;
  95. break;
  96. case "H"://指定高,宽按比例
  97. toWidth = lastPhotoWidth = originalImage.Width * photoHeight / originalImage.Height;
  98. break;
  99. case "CUT"://指定高宽裁减(不变形)
  100. if ((double)originalImage.Width / (double)originalImage.Height > (double)lastPhotoWidth / (double)lastPhotoHeight)
  101. {
  102. oh = originalImage.Height;
  103. ow = originalImage.Height * lastPhotoWidth / lastPhotoHeight;
  104. y = 0;
  105. x = (originalImage.Width - ow) / 2;
  106. }
  107. else
  108. {
  109. ow = originalImage.Width;
  110. oh = originalImage.Width * photoHeight / lastPhotoWidth;
  111. x = 0;
  112. y = (originalImage.Height - oh) / 2;
  113. }
  114. break;
  115. default:
  116. break;
  117. }
  118. System.Drawing.Image bitmap = new System.Drawing.Bitmap(lastPhotoWidth, lastPhotoHeight);//新建一个bmp图片
  119. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);//新建一个画板
  120. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
  121. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
  122. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  123. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  124. g.Clear(System.Drawing.Color.White);//白色
  125. g.DrawImage(originalImage, new System.Drawing.Rectangle(bg_x, bg_y, toWidth, toHeight),//在指定位置并且按指定大小绘制原图片的指定部分
  126. new System.Drawing.Rectangle(x, y, ow, oh),
  127. System.Drawing.GraphicsUnit.Pixel);
  128. try
  129. {
  130. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  131. }
  132. catch (System.Exception e)
  133. {
  134. throw e;
  135. }
  136. finally
  137. {
  138. bitmap.Dispose();
  139. g.Dispose();
  140. }
  141. #endregion
  142. }
  143. #endregion
  144. /// <summary>
  145. /// 切割后生成缩略图
  146. /// </summary>
  147. /// <param name="originalImagePath">源图路径(物理路径)</param>
  148. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  149. /// <param name="toW">缩略图最终宽度</param>
  150. /// <param name="toH">缩略图最终高度</param>
  151. /// <param name="X">X坐标(zoom为1时)</param>
  152. /// <param name="Y">Y坐标(zoom为1时)</param>
  153. /// <param name="W">选择区域宽(zoom为1时)</param>
  154. /// <param name="H">选择区域高(zoom为1时)</param>
  155. public static void MakeMyThumbs(string originalImagePath, string thumbnailPath, int toW, int toH, int X, int Y, int W, int H)
  156. {
  157. System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  158. int towidth = toW;
  159. int toheight = toH;
  160. int x = X;
  161. int y = Y;
  162. int ow = W;
  163. int oh = H;
  164. //新建一个bmp图片
  165. System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  166. //新建一个画板
  167. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  168. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
  169. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
  170. g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
  171. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  172. //清空画布并以透明背景色填充
  173. g.Clear(System.Drawing.Color.Transparent);
  174. //在指定位置并且按指定大小绘制原图片的指定部分
  175. g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
  176. new System.Drawing.Rectangle(x, y, ow, oh),
  177. System.Drawing.GraphicsUnit.Pixel);
  178. try
  179. {
  180. //以jpg格式保存缩略图
  181. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  182. }
  183. catch (System.Exception e)
  184. {
  185. throw e;
  186. }
  187. finally
  188. {
  189. originalImage.Dispose();
  190. bitmap.Dispose();
  191. g.Dispose();
  192. }
  193. }
  194. #region 在图片上增加文字水印
  195. /// <summary>
  196. /// 在图片上增加文字水印
  197. /// </summary>
  198. /// <param name="Path">原服务器图片路径</param>
  199. /// <param name="Path_sy">生成的带文字水印的图片路径</param>
  200. /// <param name="addText">水印文字</param>
  201. public static void AddWater(string Path, string Path_sy, string addText)
  202. {
  203. System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
  204. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
  205. g.DrawImage(image, 0, 0, image.Width, image.Height);
  206. System.Drawing.Font f = new System.Drawing.Font("Verdana", 60);
  207. System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
  208. g.DrawString(addText, f, b, 35, 35);
  209. g.Dispose();
  210. image.Save(Path_sy);
  211. image.Dispose();
  212. }
  213. #endregion
  214. #region 在图片上生成图片水印
  215. /// <summary>
  216. /// 加图片水印
  217. /// </summary>
  218. /// <param name="filename">文件名</param>
  219. /// <param name="watermarkFilename">水印文件名</param>
  220. /// <param name="watermarkStatus">图片水印位置:0=不使用 1=左上 2=中上 3=右上 4=左中 ... 9=右下</param>
  221. /// <param name="quality">是否是高质量图片 取值范围0--100</param>
  222. /// <param name="watermarkTransparency">图片水印透明度 取值范围1--10 (10为不透明)</param>
  223. public static void AddImageSignPic(string Path, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency)
  224. {
  225. System.Drawing.Image img = System.Drawing.Image.FromFile(Path);
  226. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
  227. //设置高质量插值法
  228. //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  229. //设置高质量,低速度呈现平滑程度
  230. //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  231. System.Drawing.Image watermark = new System.Drawing.Bitmap(watermarkFilename);
  232. if (watermark.Height >= img.Height || watermark.Width >= img.Width)
  233. {
  234. return;
  235. }
  236. System.Drawing.Imaging.ImageAttributes imageAttributes = new System.Drawing.Imaging.ImageAttributes();
  237. System.Drawing.Imaging.ColorMap colorMap = new System.Drawing.Imaging.ColorMap();
  238. colorMap.OldColor = System.Drawing.Color.FromArgb(255, 0, 255, 0);
  239. colorMap.NewColor = System.Drawing.Color.FromArgb(0, 0, 0, 0);
  240. System.Drawing.Imaging.ColorMap[] remapTable = { colorMap };
  241. imageAttributes.SetRemapTable(remapTable, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  242. float transparency = 0.5F;
  243. if (watermarkTransparency >= 1 && watermarkTransparency <= 10)
  244. {
  245. transparency = (watermarkTransparency / 10.0F);
  246. }
  247. float[][] colorMatrixElements = {
  248. new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
  249. new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
  250. new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
  251. new float[] {0.0f, 0.0f, 0.0f, transparency, 0.0f},
  252. new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
  253. };
  254. System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(colorMatrixElements);
  255. imageAttributes.SetColorMatrix(colorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
  256. int xpos = 0;
  257. int ypos = 0;
  258. switch (watermarkStatus)
  259. {
  260. case 1:
  261. xpos = (int)(img.Width * (float).01);
  262. ypos = (int)(img.Height * (float).01);
  263. break;
  264. case 2:
  265. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  266. ypos = (int)(img.Height * (float).01);
  267. break;
  268. case 3:
  269. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  270. ypos = (int)(img.Height * (float).01);
  271. break;
  272. case 4:
  273. xpos = (int)(img.Width * (float).01);
  274. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  275. break;
  276. case 5:
  277. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  278. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  279. break;
  280. case 6:
  281. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  282. ypos = (int)((img.Height * (float).50) - (watermark.Height / 2));
  283. break;
  284. case 7:
  285. xpos = (int)(img.Width * (float).01);
  286. ypos = (int)((img.Height * (float).99) - watermark.Height);
  287. break;
  288. case 8:
  289. xpos = (int)((img.Width * (float).50) - (watermark.Width / 2));
  290. ypos = (int)((img.Height * (float).99) - watermark.Height);
  291. break;
  292. case 9:
  293. xpos = (int)((img.Width * (float).99) - (watermark.Width));
  294. ypos = (int)((img.Height * (float).99) - watermark.Height);
  295. break;
  296. }
  297. g.DrawImage(watermark, new System.Drawing.Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, System.Drawing.GraphicsUnit.Pixel, imageAttributes);
  298. System.Drawing.Imaging.ImageCodecInfo[] codecs = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
  299. System.Drawing.Imaging.ImageCodecInfo ici = null;
  300. foreach (System.Drawing.Imaging.ImageCodecInfo codec in codecs)
  301. {
  302. //if (codec.MimeType.IndexOf("jpeg") > -1)
  303. if (codec.MimeType.Contains("jpeg"))
  304. {
  305. ici = codec;
  306. }
  307. }
  308. System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
  309. long[] qualityParam = new long[1];
  310. if (quality < 0 || quality > 100)
  311. {
  312. quality = 80;
  313. }
  314. qualityParam[0] = quality;
  315. System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
  316. encoderParams.Param[0] = encoderParam;
  317. if (ici != null)
  318. {
  319. img.Save(filename, ici, encoderParams);
  320. }
  321. else
  322. {
  323. img.Save(filename);
  324. }
  325. g.Dispose();
  326. img.Dispose();
  327. watermark.Dispose();
  328. imageAttributes.Dispose();
  329. }
  330. /// <summary>
  331. /// 在图片上生成图片水印
  332. /// </summary>
  333. /// <param name="Path">原服务器图片路径</param>
  334. /// <param name="Path_syp">生成的带图片水印的图片路径</param>
  335. /// <param name="Path_sypf">水印图片路径</param>
  336. public static void AddWaterPic(string Path, string Path_syp, string Path_sypf)
  337. {
  338. System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
  339. System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
  340. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
  341. g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
  342. g.Dispose();
  343. image.Save(Path_syp);
  344. image.Dispose();
  345. }
  346. #endregion
  347. }
  348. public class ChangePicToSave
  349. {
  350. private int width;
  351. private int height;
  352. private System.Drawing.Bitmap newpic, savepic;
  353. private float k1, k2;
  354. /// <summary>
  355. /// 构造函数
  356. /// </summary>
  357. public ChangePicToSave()
  358. {
  359. }
  360. /// <summary>
  361. /// 保存一张小图,一张大图,和一张原图
  362. /// </summary>
  363. /// <param name="TheImage">传入的图片对象</param>
  364. /// <param name="path">保存的文件名和路径</param>
  365. public void SaveThreePicture(object TheImage, string path)
  366. {
  367. string pic_larger;
  368. string pic_smaller;
  369. pic_larger = path.Substring(0, path.IndexOf("."));
  370. pic_larger += "_l.jpg";
  371. pic_smaller = path.Substring(0, path.IndexOf("."));
  372. pic_smaller += "_s.jpg";
  373. ChangePicToSave cp = new ChangePicToSave();
  374. cp.ChangePicToSaveMethod(TheImage, 200, 200, pic_smaller);
  375. cp.ChangePicToSaveMethod(TheImage, 800, 800, pic_larger);
  376. }
  377. /// <summary>
  378. /// 保存一张图片,并设定它的大小
  379. /// </summary>
  380. /// <param name="TheImage">传入的图片对象</param>
  381. /// <param name="TheWidth">图片宽的范围</param>
  382. /// <param name="TheHeight">图片长的范围</param>
  383. /// <param name="path">保存的文件名和路径</param>
  384. public void ChangePicToSaveMethod(object TheImage, int TheWidth, int TheHeight, string path)
  385. {
  386. newpic = new System.Drawing.Bitmap((System.Drawing.Image)TheImage);
  387. this.width = newpic.Width;
  388. this.height = newpic.Height;
  389. k1 = (float)width / (float)TheWidth;
  390. k2 = (float)height / (float)TheHeight;
  391. if (k1 > k2)
  392. {
  393. this.width = (int)(width / k1);
  394. this.height = (int)(height / k1);
  395. }
  396. else
  397. {
  398. this.width = (int)(width / k2);
  399. this.height = (int)(height / k2);
  400. }
  401. savepic = new System.Drawing.Bitmap(newpic, width, height);
  402. savepic.Save(@path);
  403. }
  404. }
更多.net通用类库 http://download.csdn.net/detail/jim_qiang/6744405
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/125176
推荐阅读
相关标签
  

闽ICP备14008679号