ImageUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using GeekDesk.Constant;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. using System.Windows;
  9. using System.Windows.Interop;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. namespace GeekDesk.Util
  13. {
  14. class ImageUtil
  15. {
  16. private static readonly string SYSTEM_ITEM = "::{.*}";
  17. /// <summary>
  18. /// 图片数组转 BitmapImage
  19. /// </summary>
  20. /// <param name="array"></param>
  21. /// <returns></returns>
  22. public static BitmapImage ByteArrToImage(byte[] array)
  23. {
  24. using (var ms = new System.IO.MemoryStream(array))
  25. {
  26. BitmapImage image = new BitmapImage();
  27. image.BeginInit();
  28. image.CacheOption = BitmapCacheOption.OnLoad; // here
  29. RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.LowQuality);
  30. image.StreamSource = ms;
  31. image.EndInit();
  32. return image;
  33. }
  34. }
  35. /// <summary>
  36. /// BitmapImage 转数组
  37. /// </summary>
  38. /// <param name="bi"></param>
  39. /// <returns></returns>
  40. public static byte[] BitmapImageToByte(BitmapImage bi)
  41. {
  42. using (MemoryStream memStream = new MemoryStream())
  43. {
  44. PngBitmapEncoder encoder = new PngBitmapEncoder();
  45. encoder.Frames.Add(BitmapFrame.Create(bi));
  46. encoder.Save(memStream);
  47. return memStream.GetBuffer();
  48. }
  49. }
  50. /// <summary>
  51. /// byte[]转换成Image
  52. /// </summary>
  53. /// <param name="byteArrayIn">二进制图片流</param>
  54. /// <returns>Image</returns>
  55. public static Image ByteArrayToImage(byte[] byteArrayIn)
  56. {
  57. if (byteArrayIn == null)
  58. return null;
  59. using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
  60. {
  61. System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
  62. ms.Flush();
  63. return returnImage;
  64. }
  65. }
  66. /// <summary>
  67. /// 图片base64 转 BitmapImage
  68. /// </summary>
  69. /// <param name="base64"></param>
  70. /// <returns></returns>
  71. public static BitmapImage Base64ToBitmapImage(string base64)
  72. {
  73. byte[] byteBuffer = Convert.FromBase64String(base64);
  74. return ByteArrToImage(byteBuffer);
  75. }
  76. /// <summary>
  77. /// 获取文件 icon
  78. /// </summary>
  79. /// <param name="filePath">文件路径</param>
  80. /// <returns></returns>
  81. public static BitmapImage GetBitmapIconByPath(string filePath)
  82. {
  83. if (File.Exists(filePath) || IsSystemItem(filePath))
  84. {
  85. if (IsImage(filePath)) {
  86. //图片
  87. return GetThumbnailByFile(filePath, 256, 256);
  88. } else
  89. { //其它文件
  90. return FileIcon.GetBitmapImage(filePath);
  91. }
  92. } else if(Directory.Exists(filePath)) {
  93. //文件夹
  94. return ImageUtil.Base64ToBitmapImage(Constants.DEFAULT_DIR_IMAGE_BASE64);
  95. }
  96. return null;
  97. }
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. /// <param name="lcFilename">需要改变大小的图片位置</param>
  102. /// <param name="lnWidth">缩略图的宽度</param>
  103. /// <param name="lnHeight">缩略图的高度</param>
  104. /// <returns></returns>
  105. //public static BitmapImage GetThumbnail(string lcFilename, int lnWidth, int lnHeight)
  106. //{
  107. // Bitmap bmpOut = null;
  108. // try
  109. // {
  110. // Bitmap loBMP = new Bitmap(lcFilename);
  111. // ImageFormat loFormat = loBMP.RawFormat;
  112. // decimal lnRatio;
  113. // int lnNewWidth = 0;
  114. // int lnNewHeight = 0;
  115. // //如果图像小于缩略图直接返回原图,因为upfront
  116. // if (loBMP.Width < lnWidth && loBMP.Height < lnHeight)
  117. // return BitmapToBitmapImage(loBMP);
  118. // if (loBMP.Width > loBMP.Height)
  119. // {
  120. // lnRatio = (decimal)lnWidth / loBMP.Width;
  121. // lnNewWidth = lnWidth;
  122. // decimal lnTemp = loBMP.Height * lnRatio;
  123. // lnNewHeight = (int)lnTemp;
  124. // }
  125. // else
  126. // {
  127. // lnRatio = (decimal)lnHeight / loBMP.Height;
  128. // lnNewHeight = lnHeight;
  129. // decimal lnTemp = loBMP.Width * lnRatio;
  130. // lnNewWidth = (int)lnTemp;
  131. // }
  132. // bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
  133. // Graphics g = Graphics.FromImage(bmpOut);
  134. // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  135. // g.FillRectangle(Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
  136. // g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
  137. // loBMP.Dispose();
  138. // }
  139. // catch (Exception e)
  140. // {
  141. // return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
  142. // }
  143. // return BitmapToBitmapImage(bmpOut);
  144. //}
  145. public static BitmapImage GetThumbnailByFile(string filePath, int tWidth, int tHeight)
  146. {
  147. try
  148. {
  149. Image img = Image.FromFile(filePath);
  150. if (img.Width <= tWidth && img.Height <= tHeight)
  151. {
  152. return GetBitmapImageByFile(filePath);
  153. }
  154. else
  155. {
  156. Bitmap loBMP = new Bitmap(filePath);
  157. ImageFormat loFormat = loBMP.RawFormat;
  158. decimal lnRatio;
  159. int lnNewWidth;
  160. int lnNewHeight;
  161. if (loBMP.Width > loBMP.Height)
  162. {
  163. lnRatio = (decimal)tWidth / loBMP.Width;
  164. lnNewWidth = tWidth;
  165. decimal lnTemp = loBMP.Height * lnRatio;
  166. lnNewHeight = (int)lnTemp;
  167. }
  168. else
  169. {
  170. lnRatio = (decimal)tHeight / loBMP.Height;
  171. lnNewHeight = tHeight;
  172. decimal lnTemp = loBMP.Width * lnRatio;
  173. lnNewWidth = (int)lnTemp;
  174. }
  175. Bitmap bmpOut = new Bitmap(lnNewWidth, lnNewHeight);
  176. Graphics g = Graphics.FromImage(bmpOut);
  177. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
  178. g.FillRectangle(System.Drawing.Brushes.White, 0, 0, lnNewWidth, lnNewHeight);
  179. g.DrawImage(loBMP, 0, 0, lnNewWidth, lnNewHeight);
  180. loBMP.Dispose();
  181. string tempPath = Constants.APP_DIR + "\\temp";
  182. if (File.Exists(tempPath))
  183. {
  184. File.Delete(tempPath);
  185. }
  186. bmpOut.Save(tempPath, loFormat);
  187. BitmapImage bm = GetBitmapImageByFile(tempPath);
  188. File.Delete(tempPath);
  189. return bm;
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. LogUtil.WriteErrorLog(ex, "获取缩略图失败!filePath=" + filePath);
  195. return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
  196. }
  197. }
  198. public static BitmapImage GetBitmapImageByFile(string filePath)
  199. {
  200. BitmapImage bmImg = new BitmapImage();
  201. bmImg.BeginInit();
  202. bmImg.CacheOption = BitmapCacheOption.OnLoad;
  203. RenderOptions.SetBitmapScalingMode(bmImg, BitmapScalingMode.LowQuality);
  204. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  205. {
  206. bmImg.StreamSource = fs;
  207. bmImg.EndInit();
  208. }
  209. return bmImg;
  210. }
  211. public static BitmapImage MemoryStremToBitMapImage(MemoryStream ms)
  212. {
  213. BitmapImage bi = new BitmapImage();
  214. bi.BeginInit();
  215. bi.StreamSource = ms;
  216. bi.CacheOption = BitmapCacheOption.OnLoad;
  217. bi.EndInit();
  218. bi.Freeze();
  219. return bi;
  220. }
  221. /// <summary>
  222. /// Bitmap to BitmapImage
  223. /// </summary>
  224. /// <param name="bitmap"></param>
  225. /// <returns></returns>
  226. public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
  227. {
  228. return BitmapToBitmapImage(bitmap, null);
  229. }
  230. public static BitmapImage BitmapToBitmapImage(Image bitmap, ImageFormat format)
  231. {
  232. BitmapImage bitmapImage = new BitmapImage();
  233. using (MemoryStream ms = new MemoryStream())
  234. {
  235. if (format == null)
  236. {
  237. bitmap.Save(ms, bitmap.RawFormat);
  238. }
  239. else
  240. {
  241. bitmap.Save(ms, format);
  242. }
  243. bitmapImage.BeginInit();
  244. bitmapImage.StreamSource = ms;
  245. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  246. bitmapImage.EndInit();
  247. bitmapImage.Freeze();
  248. }
  249. return bitmapImage;
  250. }
  251. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  252. public static extern bool DeleteObject(IntPtr hObject);
  253. public static BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
  254. {
  255. MemoryStream ms = new MemoryStream();
  256. bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  257. BitmapImage bit3 = new BitmapImage();
  258. bit3.BeginInit();
  259. bit3.StreamSource = ms;
  260. bit3.EndInit();
  261. return bit3;
  262. }
  263. /// <summary>
  264. /// 图片文件转base64
  265. /// </summary>
  266. /// <param name="Imagefilename"></param>
  267. /// <returns></returns>
  268. public static string FileImageToBase64(string Imagefilename, ImageFormat format)
  269. {
  270. try
  271. {
  272. Bitmap bmp = new Bitmap(Imagefilename);
  273. MemoryStream ms = new MemoryStream();
  274. bmp.Save(ms, format);
  275. byte[] arr = new byte[ms.Length];
  276. ms.Position = 0;
  277. ms.Read(arr, 0, (int)ms.Length);
  278. ms.Close();
  279. return Convert.ToBase64String(arr);
  280. }
  281. catch (Exception ex)
  282. {
  283. LogUtil.WriteErrorLog(ex, "文件转base64失败!Imagefilename=" + Imagefilename);
  284. return null;
  285. }
  286. }
  287. /// <summary>
  288. /// 判断文件是否为图片
  289. /// </summary>
  290. /// <param name="path">文件路径</param>
  291. /// <returns></returns>
  292. public static bool IsImage(string path)
  293. {
  294. try
  295. {
  296. string strExt = Path.GetExtension(path).Substring(1);
  297. string suffixs = "bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp,avif";
  298. string[] suffixArr = suffixs.Split(',');
  299. foreach (string suffix in suffixArr)
  300. {
  301. if (suffix.Equals(strExt, StringComparison.InvariantCultureIgnoreCase))
  302. {
  303. return true;
  304. }
  305. }
  306. return false;
  307. }
  308. catch (Exception)
  309. {
  310. return false;
  311. }
  312. }
  313. /// <summary>
  314. /// 判断是否为系统项
  315. /// </summary>
  316. /// <returns></returns>
  317. public static bool IsSystemItem(string path)
  318. {
  319. return Regex.IsMatch(path, SYSTEM_ITEM);
  320. }
  321. }
  322. }