ImageUtil.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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)
  193. {
  194. return Base64ToBitmapImage(Constants.DEFAULT_IMG_IMAGE_BASE64);
  195. }
  196. }
  197. public static BitmapImage GetBitmapImageByFile(string filePath)
  198. {
  199. BitmapImage bmImg = new BitmapImage();
  200. bmImg.BeginInit();
  201. bmImg.CacheOption = BitmapCacheOption.OnLoad;
  202. RenderOptions.SetBitmapScalingMode(bmImg, BitmapScalingMode.LowQuality);
  203. using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
  204. {
  205. bmImg.StreamSource = fs;
  206. bmImg.EndInit();
  207. }
  208. return bmImg;
  209. }
  210. public static BitmapImage MemoryStremToBitMapImage(MemoryStream ms)
  211. {
  212. BitmapImage bi = new BitmapImage();
  213. bi.BeginInit();
  214. bi.StreamSource = ms;
  215. bi.CacheOption = BitmapCacheOption.OnLoad;
  216. bi.EndInit();
  217. bi.Freeze();
  218. return bi;
  219. }
  220. /// <summary>
  221. /// Bitmap to BitmapImage
  222. /// </summary>
  223. /// <param name="bitmap"></param>
  224. /// <returns></returns>
  225. public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
  226. {
  227. return BitmapToBitmapImage(bitmap, null);
  228. }
  229. public static BitmapImage BitmapToBitmapImage(Image bitmap, ImageFormat format)
  230. {
  231. BitmapImage bitmapImage = new BitmapImage();
  232. using (MemoryStream ms = new MemoryStream())
  233. {
  234. if (format == null)
  235. {
  236. bitmap.Save(ms, bitmap.RawFormat);
  237. }
  238. else
  239. {
  240. bitmap.Save(ms, format);
  241. }
  242. bitmapImage.BeginInit();
  243. bitmapImage.StreamSource = ms;
  244. bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  245. bitmapImage.EndInit();
  246. bitmapImage.Freeze();
  247. }
  248. return bitmapImage;
  249. }
  250. [System.Runtime.InteropServices.DllImport("gdi32.dll")]
  251. public static extern bool DeleteObject(IntPtr hObject);
  252. public static BitmapImage Bitmap2BitmapImage(Bitmap bitmap)
  253. {
  254. MemoryStream ms = new MemoryStream();
  255. bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  256. BitmapImage bit3 = new BitmapImage();
  257. bit3.BeginInit();
  258. bit3.StreamSource = ms;
  259. bit3.EndInit();
  260. return bit3;
  261. }
  262. /// <summary>
  263. /// 图片文件转base64
  264. /// </summary>
  265. /// <param name="Imagefilename"></param>
  266. /// <returns></returns>
  267. public static string FileImageToBase64(string Imagefilename, ImageFormat format)
  268. {
  269. try
  270. {
  271. Bitmap bmp = new Bitmap(Imagefilename);
  272. MemoryStream ms = new MemoryStream();
  273. bmp.Save(ms, format);
  274. byte[] arr = new byte[ms.Length];
  275. ms.Position = 0;
  276. ms.Read(arr, 0, (int)ms.Length);
  277. ms.Close();
  278. return Convert.ToBase64String(arr);
  279. }
  280. catch (Exception)
  281. {
  282. return null;
  283. }
  284. }
  285. /// <summary>
  286. /// 判断文件是否为图片
  287. /// </summary>
  288. /// <param name="path">文件路径</param>
  289. /// <returns></returns>
  290. public static bool IsImage(string path)
  291. {
  292. try
  293. {
  294. string strExt = Path.GetExtension(path).Substring(1);
  295. string suffixs = "bmp,jpg,png,tif,gif,pcx,tga,exif,fpx,svg,psd,cdr,pcd,dxf,ufo,eps,ai,raw,WMF,webp,avif";
  296. string[] suffixArr = suffixs.Split(',');
  297. foreach (string suffix in suffixArr)
  298. {
  299. if (suffix.Equals(strExt, StringComparison.InvariantCultureIgnoreCase))
  300. {
  301. return true;
  302. }
  303. }
  304. return false;
  305. }
  306. catch (Exception)
  307. {
  308. return false;
  309. }
  310. }
  311. /// <summary>
  312. /// 判断是否为系统项
  313. /// </summary>
  314. /// <returns></returns>
  315. public static bool IsSystemItem(string path)
  316. {
  317. return Regex.IsMatch(path, SYSTEM_ITEM);
  318. }
  319. }
  320. }