PictureConverter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace PDF_Master.Helper
  8. {
  9. public class PictureConverter
  10. {
  11. /// <summary>
  12. /// 图片转PNG(其他图片先转为PNG在转JPG)
  13. /// picturefile 图片文件名
  14. /// filePath 保存路径例如//filePath.png
  15. /// </summary>
  16. public static bool SavePng(string picturefile, string filePath)
  17. {
  18. Image img = Image.FromFile(picturefile);
  19. try
  20. {
  21. using (var bmp = new Bitmap(img.Width, img.Height))
  22. {
  23. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  24. using (var g = Graphics.FromImage(bmp))
  25. {
  26. g.Clear(Color.White);
  27. g.DrawImageUnscaled(img, 0, 0);
  28. }
  29. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  30. }
  31. return true;
  32. }
  33. catch
  34. {
  35. return false;
  36. }
  37. }
  38. /// <summary>
  39. /// 图片转PNG(其他图片先转为PNG在转JPG)
  40. /// picturefile 图片image
  41. /// filePath 保存路径例如//filePath.png
  42. /// </summary>
  43. public static bool SavePng(Image img, string filePath)
  44. {
  45. try
  46. {
  47. using (var bmp = new Bitmap(img.Width, img.Height))
  48. {
  49. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  50. using (var g = Graphics.FromImage(bmp))
  51. {
  52. g.Clear(Color.White);
  53. g.DrawImageUnscaled(img, 0, 0);
  54. }
  55. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  56. }
  57. return true;
  58. }
  59. catch
  60. {
  61. return false;
  62. }
  63. }
  64. /// <summary>
  65. /// 图片转JPeG
  66. /// picturefile 图片文件名
  67. /// filePath 保存路径例如//filePath.jpg
  68. /// </summary>
  69. public static bool SaveJpeg(string picturefile, string filePath)
  70. {
  71. Image img = Image.FromFile(picturefile);
  72. try
  73. {
  74. using (var bmp = new Bitmap(img.Width, img.Height))
  75. {
  76. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  77. using (var g = Graphics.FromImage(bmp))
  78. {
  79. g.Clear(Color.White);
  80. g.DrawImageUnscaled(img, 0, 0);
  81. }
  82. //存储各种格式
  83. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
  84. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  85. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  86. }
  87. return true;
  88. }
  89. catch
  90. {
  91. return false;
  92. }
  93. }
  94. /// <summary>
  95. /// 图片转JPeG
  96. /// img 图片Image
  97. /// filePath 保存路径例如//filePath.jpg
  98. /// </summary>
  99. public static bool SaveJpeg(Image img, string filePath)
  100. {
  101. try
  102. {
  103. using (var bmp = new Bitmap(img.Width, img.Height))
  104. {
  105. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  106. using (var g = Graphics.FromImage(bmp))
  107. {
  108. g.Clear(Color.White);
  109. g.DrawImageUnscaled(img, 0, 0);
  110. }
  111. //存储各种格式
  112. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
  113. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  114. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  115. }
  116. return true;
  117. }
  118. catch
  119. {
  120. return false;
  121. }
  122. }
  123. }
  124. }