查看: 2293|回复: 0

[.NET开发] C#图片处理类分享

发表于 2018-2-9 08:00:00

本文实例为大家分享了C#图片处理类的具体代码,供大家参考,具体内容如下

  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Drawing.Drawing2D;
  7. namespace DotNet.Utilities
  8. {
  9. public class ImageClass
  10. {
  11. public ImageClass()
  12. { }
  13. #region 缩略图
  14. /// <summary>
  15. /// 生成缩略图
  16. /// </summary>
  17. /// <param name="originalImagePath">源图路径(物理路径)</param>
  18. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  19. /// <param name="width">缩略图宽度</param>
  20. /// <param name="height">缩略图高度</param>
  21. /// <param name="mode">生成缩略图的方式</param>
  22. public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
  23. {
  24. System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
  25. int towidth = width;
  26. int toheight = height;
  27. int x = 0;
  28. int y = 0;
  29. int ow = originalImage.Width;
  30. int oh = originalImage.Height;
  31. switch (mode)
  32. {
  33. case "HW": //指定高宽缩放(可能变形)
  34. break;
  35. case "W": //指定宽,高按比例
  36. toheight = originalImage.Height * width / originalImage.Width;
  37. break;
  38. case "H": //指定高,宽按比例
  39. towidth = originalImage.Width * height / originalImage.Height;
  40. break;
  41. case "Cut": //指定高宽裁减(不变形)
  42. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  43. {
  44. oh = originalImage.Height;
  45. ow = originalImage.Height * towidth / toheight;
  46. y = 0;
  47. x = (originalImage.Width - ow) / 2;
  48. }
  49. else
  50. {
  51. ow = originalImage.Width;
  52. oh = originalImage.Width * height / towidth;
  53. x = 0;
  54. y = (originalImage.Height - oh) / 2;
  55. }
  56. break;
  57. default:
  58. break;
  59. }
  60. //新建一个bmp图片
  61. System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
  62. //新建一个画板
  63. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
  64. //设置高质量插值法
  65. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
  66. //设置高质量,低速度呈现平滑程度
  67. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  68. //清空画布并以透明背景色填充
  69. g.Clear(System.Drawing.Color.Transparent);
  70. //在指定位置并且按指定大小绘制原图片的指定部分
  71. g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
  72. try
  73. {
  74. //以jpg格式保存缩略图
  75. bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  76. }
  77. catch (System.Exception e)
  78. {
  79. throw e;
  80. }
  81. finally
  82. {
  83. originalImage.Dispose();
  84. bitmap.Dispose();
  85. g.Dispose();
  86. }
  87. }
  88. #endregion
  89. #region 图片水印
  90. /// <summary>
  91. /// 图片水印处理方法
  92. /// </summary>
  93. /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
  94. /// <param name="waterpath">水印图片(绝对路径)</param>
  95. /// <param name="location">水印位置(传送正确的代码)</param>
  96. public static string ImageWatermark(string path, string waterpath, string location)
  97. {
  98. string kz_name = Path.GetExtension(path);
  99. if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
  100. {
  101. DateTime time = DateTime.Now;
  102. string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
  103. Image img = Bitmap.FromFile(path);
  104. Image waterimg = Image.FromFile(waterpath);
  105. Graphics g = Graphics.FromImage(img);
  106. ArrayList loca = GetLocation(location, img, waterimg);
  107. g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
  108. waterimg.Dispose();
  109. g.Dispose();
  110. string newpath = Path.GetDirectoryName(path) + filename + kz_name;
  111. img.Save(newpath);
  112. img.Dispose();
  113. File.Copy(newpath, path, true);
  114. if (File.Exists(newpath))
  115. {
  116. File.Delete(newpath);
  117. }
  118. }
  119. return path;
  120. }
  121. /// <summary>
  122. /// 图片水印位置处理方法
  123. /// </summary>
  124. /// <param name="location">水印位置</param>
  125. /// <param name="img">需要添加水印的图片</param>
  126. /// <param name="waterimg">水印图片</param>
  127. private static ArrayList GetLocation(string location, Image img, Image waterimg)
  128. {
  129. ArrayList loca = new ArrayList();
  130. int x = 0;
  131. int y = 0;
  132. if (location == "LT")
  133. {
  134. x = 10;
  135. y = 10;
  136. }
  137. else if (location == "T")
  138. {
  139. x = img.Width / 2 - waterimg.Width / 2;
  140. y = img.Height - waterimg.Height;
  141. }
  142. else if (location == "RT")
  143. {
  144. x = img.Width - waterimg.Width;
  145. y = 10;
  146. }
  147. else if (location == "LC")
  148. {
  149. x = 10;
  150. y = img.Height / 2 - waterimg.Height / 2;
  151. }
  152. else if (location == "C")
  153. {
  154. x = img.Width / 2 - waterimg.Width / 2;
  155. y = img.Height / 2 - waterimg.Height / 2;
  156. }
  157. else if (location == "RC")
  158. {
  159. x = img.Width - waterimg.Width;
  160. y = img.Height / 2 - waterimg.Height / 2;
  161. }
  162. else if (location == "LB")
  163. {
  164. x = 10;
  165. y = img.Height - waterimg.Height;
  166. }
  167. else if (location == "B")
  168. {
  169. x = img.Width / 2 - waterimg.Width / 2;
  170. y = img.Height - waterimg.Height;
  171. }
  172. else
  173. {
  174. x = img.Width - waterimg.Width;
  175. y = img.Height - waterimg.Height;
  176. }
  177. loca.Add(x);
  178. loca.Add(y);
  179. return loca;
  180. }
  181. #endregion
  182. #region 文字水印
  183. /// <summary>
  184. /// 文字水印处理方法
  185. /// </summary>
  186. /// <param name="path">图片路径(绝对路径)</param>
  187. /// <param name="size">字体大小</param>
  188. /// <param name="letter">水印文字</param>
  189. /// <param name="color">颜色</param>
  190. /// <param name="location">水印位置</param>
  191. public static string LetterWatermark(string path, int size, string letter, Color color, string location)
  192. {
  193. #region
  194. string kz_name = Path.GetExtension(path);
  195. if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg")
  196. {
  197. DateTime time = DateTime.Now;
  198. string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString();
  199. Image img = Bitmap.FromFile(path);
  200. Graphics gs = Graphics.FromImage(img);
  201. ArrayList loca = GetLocation(location, img, size, letter.Length);
  202. Font font = new Font("宋体", size);
  203. Brush br = new SolidBrush(color);
  204. gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString()));
  205. gs.Dispose();
  206. string newpath = Path.GetDirectoryName(path) + filename + kz_name;
  207. img.Save(newpath);
  208. img.Dispose();
  209. File.Copy(newpath, path, true);
  210. if (File.Exists(newpath))
  211. {
  212. File.Delete(newpath);
  213. }
  214. }
  215. return path;
  216. #endregion
  217. }
  218. /// <summary>
  219. /// 文字水印位置的方法
  220. /// </summary>
  221. /// <param name="location">位置代码</param>
  222. /// <param name="img">图片对象</param>
  223. /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param>
  224. /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param>
  225. private static ArrayList GetLocation(string location, Image img, int width, int height)
  226. {
  227. #region
  228. ArrayList loca = new ArrayList(); //定义数组存储位置
  229. float x = 10;
  230. float y = 10;
  231. if (location == "LT")
  232. {
  233. loca.Add(x);
  234. loca.Add(y);
  235. }
  236. else if (location == "T")
  237. {
  238. x = img.Width / 2 - (width * height) / 2;
  239. loca.Add(x);
  240. loca.Add(y);
  241. }
  242. else if (location == "RT")
  243. {
  244. x = img.Width - width * height;
  245. }
  246. else if (location == "LC")
  247. {
  248. y = img.Height / 2;
  249. }
  250. else if (location == "C")
  251. {
  252. x = img.Width / 2 - (width * height) / 2;
  253. y = img.Height / 2;
  254. }
  255. else if (location == "RC")
  256. {
  257. x = img.Width - height;
  258. y = img.Height / 2;
  259. }
  260. else if (location == "LB")
  261. {
  262. y = img.Height - width - 5;
  263. }
  264. else if (location == "B")
  265. {
  266. x = img.Width / 2 - (width * height) / 2;
  267. y = img.Height - width - 5;
  268. }
  269. else
  270. {
  271. x = img.Width - width * height;
  272. y = img.Height - width - 5;
  273. }
  274. loca.Add(x);
  275. loca.Add(y);
  276. return loca;
  277. #endregion
  278. }
  279. #endregion
  280. #region 调整光暗
  281. /// <summary>
  282. /// 调整光暗
  283. /// </summary>
  284. /// <param name="mybm">原始图片</param>
  285. /// <param name="width">原始图片的长度</param>
  286. /// <param name="height">原始图片的高度</param>
  287. /// <param name="val">增加或减少的光暗值</param>
  288. public Bitmap LDPic(Bitmap mybm, int width, int height, int val)
  289. {
  290. Bitmap bm = new Bitmap(width, height);//初始化一个记录经过处理后的图片对象
  291. int x, y, resultR, resultG, resultB;//x、y是循环次数,后面三个是记录红绿蓝三个值的
  292. Color pixel;
  293. for (x = 0; x < width; x++)
  294. {
  295. for (y = 0; y < height; y++)
  296. {
  297. pixel = mybm.GetPixel(x, y);//获取当前像素的值
  298. resultR = pixel.R + val;//检查红色值会不会超出[0, 255]
  299. resultG = pixel.G + val;//检查绿色值会不会超出[0, 255]
  300. resultB = pixel.B + val;//检查蓝色值会不会超出[0, 255]
  301. bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
  302. }
  303. }
  304. return bm;
  305. }
  306. #endregion
  307. #region 反色处理
  308. /// <summary>
  309. /// 反色处理
  310. /// </summary>
  311. /// <param name="mybm">原始图片</param>
  312. /// <param name="width">原始图片的长度</param>
  313. /// <param name="height">原始图片的高度</param>
  314. public Bitmap RePic(Bitmap mybm, int width, int height)
  315. {
  316. Bitmap bm = new Bitmap(width, height);//初始化一个记录处理后的图片的对象
  317. int x, y, resultR, resultG, resultB;
  318. Color pixel;
  319. for (x = 0; x < width; x++)
  320. {
  321. for (y = 0; y < height; y++)
  322. {
  323. pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
  324. resultR = 255 - pixel.R;//反红
  325. resultG = 255 - pixel.G;//反绿
  326. resultB = 255 - pixel.B;//反蓝
  327. bm.SetPixel(x, y, Color.FromArgb(resultR, resultG, resultB));//绘图
  328. }
  329. }
  330. return bm;
  331. }
  332. #endregion
  333. #region 浮雕处理
  334. /// <summary>
  335. /// 浮雕处理
  336. /// </summary>
  337. /// <param name="oldBitmap">原始图片</param>
  338. /// <param name="Width">原始图片的长度</param>
  339. /// <param name="Height">原始图片的高度</param>
  340. public Bitmap FD(Bitmap oldBitmap, int Width, int Height)
  341. {
  342. Bitmap newBitmap = new Bitmap(Width, Height);
  343. Color color1, color2;
  344. for (int x = 0; x < Width - 1; x++)
  345. {
  346. for (int y = 0; y < Height - 1; y++)
  347. {
  348. int r = 0, g = 0, b = 0;
  349. color1 = oldBitmap.GetPixel(x, y);
  350. color2 = oldBitmap.GetPixel(x + 1, y + 1);
  351. r = Math.Abs(color1.R - color2.R + 128);
  352. g = Math.Abs(color1.G - color2.G + 128);
  353. b = Math.Abs(color1.B - color2.B + 128);
  354. if (r > 255) r = 255;
  355. if (r < 0) r = 0;
  356. if (g > 255) g = 255;
  357. if (g < 0) g = 0;
  358. if (b > 255) b = 255;
  359. if (b < 0) b = 0;
  360. newBitmap.SetPixel(x, y, Color.FromArgb(r, g, b));
  361. }
  362. }
  363. return newBitmap;
  364. }
  365. #endregion
  366. #region 拉伸图片
  367. /// <summary>
  368. /// 拉伸图片
  369. /// </summary>
  370. /// <param name="bmp">原始图片</param>
  371. /// <param name="newW">新的宽度</param>
  372. /// <param name="newH">新的高度</param>
  373. public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
  374. {
  375. try
  376. {
  377. Bitmap bap = new Bitmap(newW, newH);
  378. Graphics g = Graphics.FromImage(bap);
  379. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  380. g.DrawImage(bap, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bap.Width, bap.Height), GraphicsUnit.Pixel);
  381. g.Dispose();
  382. return bap;
  383. }
  384. catch
  385. {
  386. return null;
  387. }
  388. }
  389. #endregion
  390. #region 滤色处理
  391. /// <summary>
  392. /// 滤色处理
  393. /// </summary>
  394. /// <param name="mybm">原始图片</param>
  395. /// <param name="width">原始图片的长度</param>
  396. /// <param name="height">原始图片的高度</param>
  397. public Bitmap FilPic(Bitmap mybm, int width, int height)
  398. {
  399. Bitmap bm = new Bitmap(width, height);//初始化一个记录滤色效果的图片对象
  400. int x, y;
  401. Color pixel;
  402. for (x = 0; x < width; x++)
  403. {
  404. for (y = 0; y < height; y++)
  405. {
  406. pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
  407. bm.SetPixel(x, y, Color.FromArgb(0, pixel.G, pixel.B));//绘图
  408. }
  409. }
  410. return bm;
  411. }
  412. #endregion
  413. #region 左右翻转
  414. /// <summary>
  415. /// 左右翻转
  416. /// </summary>
  417. /// <param name="mybm">原始图片</param>
  418. /// <param name="width">原始图片的长度</param>
  419. /// <param name="height">原始图片的高度</param>
  420. public Bitmap RevPicLR(Bitmap mybm, int width, int height)
  421. {
  422. Bitmap bm = new Bitmap(width, height);
  423. int x, y, z; //x,y是循环次数,z是用来记录像素点的x坐标的变化的
  424. Color pixel;
  425. for (y = height - 1; y >= 0; y--)
  426. {
  427. for (x = width - 1, z = 0; x >= 0; x--)
  428. {
  429. pixel = mybm.GetPixel(x, y);//获取当前像素的值
  430. bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
  431. }
  432. }
  433. return bm;
  434. }
  435. #endregion
  436. #region 上下翻转
  437. /// <summary>
  438. /// 上下翻转
  439. /// </summary>
  440. /// <param name="mybm">原始图片</param>
  441. /// <param name="width">原始图片的长度</param>
  442. /// <param name="height">原始图片的高度</param>
  443. public Bitmap RevPicUD(Bitmap mybm, int width, int height)
  444. {
  445. Bitmap bm = new Bitmap(width, height);
  446. int x, y, z;
  447. Color pixel;
  448. for (x = 0; x < width; x++)
  449. {
  450. for (y = height - 1, z = 0; y >= 0; y--)
  451. {
  452. pixel = mybm.GetPixel(x, y);//获取当前像素的值
  453. bm.SetPixel(x, z++, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
  454. }
  455. }
  456. return bm;
  457. }
  458. #endregion
  459. #region 压缩图片
  460. /// <summary>
  461. /// 压缩到指定尺寸
  462. /// </summary>
  463. /// <param name="oldfile">原文件</param>
  464. /// <param name="newfile">新文件</param>
  465. public bool Compress(string oldfile, string newfile)
  466. {
  467. try
  468. {
  469. System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
  470. System.Drawing.Imaging.ImageFormat thisFormat = img.RawFormat;
  471. Size newSize = new Size(100, 125);
  472. Bitmap outBmp = new Bitmap(newSize.Width, newSize.Height);
  473. Graphics g = Graphics.FromImage(outBmp);
  474. g.CompositingQuality = CompositingQuality.HighQuality;
  475. g.SmoothingMode = SmoothingMode.HighQuality;
  476. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  477. g.DrawImage(img, new Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
  478. g.Dispose();
  479. EncoderParameters encoderParams = new EncoderParameters();
  480. long[] quality = new long[1];
  481. quality[0] = 100;
  482. EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
  483. encoderParams.Param[0] = encoderParam;
  484. ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
  485. ImageCodecInfo jpegICI = null;
  486. for (int x = 0; x < arrayICI.Length; x++)
  487. if (arrayICI[x].FormatDescription.Equals("JPEG"))
  488. {
  489. jpegICI = arrayICI[x]; //设置JPEG编码
  490. break;
  491. }
  492. img.Dispose();
  493. if (jpegICI != null) outBmp.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);
  494. outBmp.Dispose();
  495. return true;
  496. }
  497. catch
  498. {
  499. return false;
  500. }
  501. }
  502. #endregion
  503. #region 图片灰度化
  504. public Color Gray(Color c)
  505. {
  506. int rgb = Convert.ToInt32((double)(((0.3 * c.R) + (0.59 * c.G)) + (0.11 * c.B)));
  507. return Color.FromArgb(rgb, rgb, rgb);
  508. }
  509. #endregion
  510. #region 转换为黑白图片
  511. /// <summary>
  512. /// 转换为黑白图片
  513. /// </summary>
  514. /// <param name="mybt">要进行处理的图片</param>
  515. /// <param name="width">图片的长度</param>
  516. /// <param name="height">图片的高度</param>
  517. public Bitmap BWPic(Bitmap mybm, int width, int height)
  518. {
  519. Bitmap bm = new Bitmap(width, height);
  520. int x, y, result; //x,y是循环次数,result是记录处理后的像素值
  521. Color pixel;
  522. for (x = 0; x < width; x++)
  523. {
  524. for (y = 0; y < height; y++)
  525. {
  526. pixel = mybm.GetPixel(x, y);//获取当前坐标的像素值
  527. result = (pixel.R + pixel.G + pixel.B) / 3;//取红绿蓝三色的平均值
  528. bm.SetPixel(x, y, Color.FromArgb(result, result, result));
  529. }
  530. }
  531. return bm;
  532. }
  533. #endregion
  534. #region 获取图片中的各帧
  535. /// <summary>
  536. /// 获取图片中的各帧
  537. /// </summary>
  538. /// <param name="pPath">图片路径</param>
  539. /// <param name="pSavePath">保存路径</param>
  540. public void GetFrames(string pPath, string pSavedPath)
  541. {
  542. Image gif = Image.FromFile(pPath);
  543. FrameDimension fd = new FrameDimension(gif.FrameDimensionsList[0]);
  544. int count = gif.GetFrameCount(fd); //获取帧数(gif图片可能包含多帧,其它格式图片一般仅一帧)
  545. for (int i = 0; i < count; i++) //以Jpeg格式保存各帧
  546. {
  547. gif.SelectActiveFrame(fd, i);
  548. gif.Save(pSavedPath + "\\frame_" + i + ".jpg", ImageFormat.Jpeg);
  549. }
  550. }
  551. #endregion
  552. }
  553. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • C# 添加图片水印类实现代码
  • c#生成图片缩略图的类(2种实现思路)
  • C#实现的图片、string相互转换类分享


回复

使用道具 举报