ImageUtil.cs 12 KB

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