PictureConverter.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media.Imaging;
  9. namespace PDF_Master.Helper
  10. {
  11. public class PictureConverter
  12. {
  13. /// <summary>
  14. /// 图片转PNG(其他图片先转为PNG在转JPG)
  15. /// picturefile 图片文件名
  16. /// filePath 保存路径例如//filePath.png
  17. /// </summary>
  18. public static bool SavePng(string picturefile, string filePath)
  19. {
  20. Image img = Image.FromFile(picturefile);
  21. try
  22. {
  23. using (var bmp = new Bitmap(img.Width, img.Height))
  24. {
  25. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  26. using (var g = Graphics.FromImage(bmp))
  27. {
  28. g.Clear(Color.White);
  29. g.DrawImageUnscaled(img, 0, 0);
  30. }
  31. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  32. }
  33. return true;
  34. }
  35. catch
  36. {
  37. return false;
  38. }
  39. }
  40. /// <summary>
  41. /// 图片转PNG(其他图片先转为PNG在转JPG)
  42. /// picturefile 图片image
  43. /// filePath 保存路径例如//filePath.png
  44. /// </summary>
  45. public static bool SavePng(Image img, string filePath)
  46. {
  47. try
  48. {
  49. using (var bmp = new Bitmap(img.Width, img.Height))
  50. {
  51. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  52. using (var g = Graphics.FromImage(bmp))
  53. {
  54. g.Clear(Color.White);
  55. g.DrawImageUnscaled(img, 0, 0);
  56. }
  57. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  58. }
  59. return true;
  60. }
  61. catch
  62. {
  63. return false;
  64. }
  65. }
  66. /// <summary>
  67. /// 图片转JPeG
  68. /// picturefile 图片文件名
  69. /// filePath 保存路径例如//filePath.jpg
  70. /// </summary>
  71. public static bool SaveJpeg(string picturefile, string filePath)
  72. {
  73. Image img = Image.FromFile(picturefile);
  74. try
  75. {
  76. using (var bmp = new Bitmap(img.Width, img.Height))
  77. {
  78. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  79. using (var g = Graphics.FromImage(bmp))
  80. {
  81. g.Clear(Color.White);
  82. g.DrawImageUnscaled(img, 0, 0);
  83. }
  84. //存储各种格式
  85. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
  86. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  87. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  88. }
  89. return true;
  90. }
  91. catch
  92. {
  93. return false;
  94. }
  95. }
  96. /// <summary>
  97. /// 图片转JPeG
  98. /// img 图片Image
  99. /// filePath 保存路径例如//filePath.jpg
  100. /// </summary>
  101. public static bool SaveJpeg(Image img, string filePath)
  102. {
  103. try
  104. {
  105. using (var bmp = new Bitmap(img.Width, img.Height))
  106. {
  107. bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
  108. using (var g = Graphics.FromImage(bmp))
  109. {
  110. g.Clear(Color.White);
  111. g.DrawImageUnscaled(img, 0, 0);
  112. }
  113. //存储各种格式
  114. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
  115. //bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
  116. bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
  117. }
  118. return true;
  119. }
  120. catch
  121. {
  122. return false;
  123. }
  124. }
  125. public static byte[] GetImageByteFromPath(string ImagePath, string fileExt, out double width, out double height)
  126. {
  127. try
  128. {
  129. #region 跟图章一样的转换方法。
  130. if (ImagePath != null && ImagePath.Length > 0 && File.Exists(ImagePath))
  131. {
  132. FileStream fileData = File.OpenRead(ImagePath);
  133. BitmapFrame frame = null;
  134. width = 0;
  135. height = 0;
  136. if (fileExt.Contains(".jpg") || fileExt.Contains(".jpeg"))
  137. {
  138. JpegBitmapDecoder decoder = (JpegBitmapDecoder)JpegBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  139. frame = decoder.Frames[0];
  140. width = frame.Width;
  141. height = frame.Height;
  142. }
  143. else if (fileExt.Contains(".png"))
  144. {
  145. PngBitmapDecoder decoder = (PngBitmapDecoder)PngBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  146. frame = decoder.Frames[0];
  147. width = frame.Width;
  148. height = frame.Height;
  149. }
  150. else if (fileExt.Contains(".bmp"))
  151. {
  152. BmpBitmapDecoder decoder = (BmpBitmapDecoder)BmpBitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  153. frame = decoder.Frames[0];
  154. width = frame.Width;
  155. height = frame.Height;
  156. }
  157. else//默认采用png的解码方式
  158. {
  159. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  160. frame = decoder.Frames[0];
  161. width = frame.Width;
  162. height = frame.Height;
  163. }
  164. if (frame != null)
  165. {
  166. var ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  167. frame.CopyPixels(ImageArray, frame.PixelWidth * 4, 0);
  168. return ImageArray;
  169. }
  170. return null;
  171. }
  172. width = 0;
  173. height = 0;
  174. return null;
  175. #endregion
  176. }
  177. catch
  178. {
  179. width = 0;
  180. height = 0;
  181. return null;
  182. }
  183. }
  184. }
  185. }