You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

642 lines
22 KiB

2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
  1. using Microsoft.AspNetCore.Http;
  2. using NPOI.HPSF;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. //using System.DrawingCore;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Net.Http;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using Volo.Abp;
  16. namespace Shentun.Peis
  17. {
  18. public class ImageHelper
  19. {
  20. /// <summary>
  21. /// 将Base64字符串转换为图片并保存到本地
  22. /// </summary>
  23. /// <param name="base64Str">base64字符串</param>
  24. /// <param name="savePath">图片保存地址,如:/Content/Images/10000</param>
  25. /// <returns></returns>
  26. public static string Base64StrToImage(string base64Str, string savePath)
  27. {
  28. var ret = "";
  29. try
  30. {
  31. base64Str = base64Str.Substring(base64Str.IndexOf(",") + 1);
  32. var bitmap = Base64StrToImage(base64Str);
  33. if (bitmap != null)
  34. {
  35. string ImageSuffix = "";
  36. if (ImageFormat.Png.Guid == bitmap.RawFormat.Guid)
  37. {
  38. ImageSuffix = ".png";
  39. }
  40. else if (ImageFormat.Gif.Guid == bitmap.RawFormat.Guid)
  41. {
  42. ImageSuffix = ".gif";
  43. }
  44. else
  45. {
  46. ImageSuffix = ".jpg";
  47. }
  48. savePath = savePath + ImageSuffix;
  49. //创建文件夹
  50. var folderPath = savePath.Substring(0, savePath.LastIndexOf('/'));
  51. if (!Directory.Exists(folderPath))
  52. {
  53. Directory.CreateDirectory(folderPath);
  54. }
  55. //图片后缀格式
  56. var suffix = savePath.Substring(savePath.LastIndexOf('.') + 1, savePath.Length - savePath.LastIndexOf('.') - 1).ToLower();
  57. var suffixName = suffix == "png" ? ImageFormat.Png :
  58. suffix == "jpg" || suffix == "jpeg" ? ImageFormat.Jpeg :
  59. suffix == "bmp" ? ImageFormat.Bmp :
  60. suffix == "gif" ? ImageFormat.Gif : ImageFormat.Jpeg;
  61. //这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示
  62. var bmpNew = new Bitmap(bitmap);
  63. // bmpNew.Save(_hostingEnvironment.ContentRootPath + savePath, suffixName);
  64. var s1 = Directory.GetCurrentDirectory();
  65. string saveurl = Path.Combine(s1, savePath);
  66. bmpNew.Save(saveurl, suffixName);
  67. bmpNew.Dispose();
  68. bitmap.Dispose();
  69. ret = savePath;
  70. }
  71. else
  72. {
  73. ret = "";
  74. }
  75. }
  76. catch (Exception ex)
  77. {
  78. ret = "";
  79. }
  80. return ret;
  81. }
  82. /// <summary>
  83. /// 将Byte[]转换为图片并保存到本地
  84. /// </summary>
  85. /// <param name="base64Str">base64字符串</param>
  86. /// <param name="savePath">图片保存地址,如:/Content/Images/10000</param>
  87. /// <returns></returns>
  88. public static string ByteToImage(byte[] photo, string savePath)
  89. {
  90. var ret = "";
  91. try
  92. {
  93. MemoryStream ms = new MemoryStream(photo);
  94. Bitmap bitmap = new Bitmap(ms);
  95. ms.Close();
  96. if (bitmap != null)
  97. {
  98. string ImageSuffix = "";
  99. if (ImageFormat.Png.Guid == bitmap.RawFormat.Guid)
  100. {
  101. ImageSuffix = ".png";
  102. }
  103. else if (ImageFormat.Gif.Guid == bitmap.RawFormat.Guid)
  104. {
  105. ImageSuffix = ".gif";
  106. }
  107. else
  108. {
  109. ImageSuffix = ".jpg";
  110. }
  111. savePath = savePath + ImageSuffix;
  112. //创建文件夹
  113. var folderPath = savePath.Substring(0, savePath.LastIndexOf('/'));
  114. if (!Directory.Exists(folderPath))
  115. {
  116. Directory.CreateDirectory(folderPath);
  117. }
  118. //图片后缀格式
  119. var suffix = savePath.Substring(savePath.LastIndexOf('.') + 1, savePath.Length - savePath.LastIndexOf('.') - 1).ToLower();
  120. var suffixName = suffix == "png" ? ImageFormat.Png :
  121. suffix == "jpg" || suffix == "jpeg" ? ImageFormat.Jpeg :
  122. suffix == "bmp" ? ImageFormat.Bmp :
  123. suffix == "gif" ? ImageFormat.Gif : ImageFormat.Jpeg;
  124. //这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示
  125. var bmpNew = new Bitmap(bitmap);
  126. // bmpNew.Save(_hostingEnvironment.ContentRootPath + savePath, suffixName);
  127. var s1 = Directory.GetCurrentDirectory();
  128. string saveurl = Path.Combine(s1, savePath);
  129. bmpNew.Save(saveurl, suffixName);
  130. bmpNew.Dispose();
  131. bitmap.Dispose();
  132. ret = savePath;
  133. }
  134. else
  135. {
  136. ret = "";
  137. }
  138. }
  139. catch (Exception ex)
  140. {
  141. ret = "";
  142. }
  143. return ret;
  144. }
  145. /// <summary>
  146. /// 将Base64字符串转换为图片并保存到服务器
  147. /// </summary>
  148. /// <param name="AbsolutePath">物理路径</param>
  149. /// <param name="fileName">图片名</param>
  150. /// <param name="base64Str"></param>
  151. /// <param name="PatientRegisterId">登记ID 作为目录</param>
  152. /// <param name="RegisterCheckId">检查ID 作为目录</param>
  153. /// <returns></returns>
  154. public static string Base64StrToImageInAbsolutePath(string AbsolutePath, string fileName, string base64Str, string PatientRegisterId, string RegisterCheckId)
  155. {
  156. var ret = "";
  157. try
  158. {
  159. base64Str = base64Str.Substring(base64Str.IndexOf(",") + 1);
  160. string savePath = "";
  161. string hostPath = "";
  162. var bitmap = Base64StrToImage(base64Str);
  163. if (bitmap != null)
  164. {
  165. string ImageSuffix = "";
  166. if (ImageFormat.Png.Guid == bitmap.RawFormat.Guid)
  167. {
  168. ImageSuffix = ".png";
  169. }
  170. else if (ImageFormat.Gif.Guid == bitmap.RawFormat.Guid)
  171. {
  172. ImageSuffix = ".gif";
  173. }
  174. else if (ImageFormat.Bmp.Guid == bitmap.RawFormat.Guid)
  175. {
  176. ImageSuffix = ".bmp";
  177. }
  178. else
  179. {
  180. ImageSuffix = ".jpg";
  181. }
  182. string savaDirectory = $"{AbsolutePath}\\pacs\\{DateTime.Now.Year}\\{DateTime.Now.Month}\\{DateTime.Now.Day}\\{PatientRegisterId}\\{RegisterCheckId}";
  183. savePath = $"{savaDirectory}\\{fileName + ImageSuffix}";
  184. hostPath = $"/CheckPictureImg/pacs/{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}/{PatientRegisterId}/{RegisterCheckId}/{fileName + ImageSuffix}";
  185. if (!Directory.Exists(savaDirectory))
  186. {
  187. Directory.CreateDirectory(savaDirectory);
  188. }
  189. //图片后缀格式
  190. var suffix = savePath.Substring(savePath.LastIndexOf('.') + 1, savePath.Length - savePath.LastIndexOf('.') - 1).ToLower();
  191. var suffixName = suffix == "png" ? ImageFormat.Png :
  192. suffix == "jpg" || suffix == "jpeg" ? ImageFormat.Jpeg :
  193. suffix == "bmp" ? ImageFormat.Bmp :
  194. suffix == "gif" ? ImageFormat.Gif : ImageFormat.Jpeg;
  195. //这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示
  196. var bmpNew = new Bitmap(bitmap);
  197. // bmpNew.Save(_hostingEnvironment.ContentRootPath + savePath, suffixName);
  198. var s1 = Directory.GetCurrentDirectory();
  199. string saveurl = Path.Combine(s1, savePath);
  200. bmpNew.Save(saveurl, suffixName);
  201. bmpNew.Dispose();
  202. bitmap.Dispose();
  203. ret = hostPath;
  204. }
  205. else
  206. {
  207. ret = "";
  208. }
  209. }
  210. catch (Exception ex)
  211. {
  212. ret = "";
  213. }
  214. return ret;
  215. }
  216. public static string Base64ToImageUseAbsolutePath(string absolutePath, string fileName, string base64Str, string patientRegisterId, string registerCheckId)
  217. {
  218. var ret = "";
  219. if (string.IsNullOrWhiteSpace(absolutePath))
  220. {
  221. throw new UserFriendlyException("absolutePath不能为空");
  222. }
  223. if (string.IsNullOrWhiteSpace(fileName))
  224. {
  225. throw new UserFriendlyException("文件名不能为空");
  226. }
  227. if (string.IsNullOrWhiteSpace(base64Str))
  228. {
  229. throw new UserFriendlyException("base64Str不能为空");
  230. }
  231. if (string.IsNullOrWhiteSpace(patientRegisterId))
  232. {
  233. throw new UserFriendlyException("PatientRegisterId不能为空");
  234. }
  235. if (string.IsNullOrWhiteSpace(registerCheckId))
  236. {
  237. throw new UserFriendlyException("RegisterCheckId不能为空");
  238. }
  239. string savaDirectory = $"{absolutePath}\\pacs\\{DateTime.Now.Year}\\{DateTime.Now.Month}\\{DateTime.Now.Day}\\{patientRegisterId}\\{registerCheckId}";
  240. if (!Directory.Exists(savaDirectory))
  241. {
  242. Directory.CreateDirectory(savaDirectory);
  243. }
  244. var savePath = $"{savaDirectory}\\{fileName}";
  245. var fullFileName = Base64ToImage(base64Str, savePath);
  246. var ImageSuffix = Path.GetExtension(fullFileName);
  247. var hostPath = $"/CheckPictureImg/pacs/{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}/{patientRegisterId}/{registerCheckId}/{fileName + ImageSuffix}";
  248. ret = hostPath;
  249. return ret;
  250. }
  251. /// <summary>
  252. /// 将Base64字符串转换为图片并保存到服务器
  253. /// </summary>
  254. /// <param name="AbsolutePath">物理路径</param>
  255. /// <param name="base64Str"></param>
  256. /// <param name="PatientRegisterId">登记ID 作为目录</param>
  257. /// <param name="RegisterCheckId">检查ID 作为目录</param>
  258. /// <param name="ImageName">图片名</param>
  259. /// <returns></returns>
  260. public static string Base64StrToImageInAbsolutePath2(string AbsolutePath, IFormFile file, string PatientRegisterId, string RegisterCheckId, string ImageName)
  261. {
  262. var ret = "";
  263. try
  264. {
  265. string savePath = "";
  266. string hostPath = "";
  267. if (file == null || file.Length == 0)
  268. {
  269. throw new UserFriendlyException("图片数据有误");
  270. }
  271. var ImageSuffix = Path.GetExtension(file.FileName);
  272. if (ImageSuffix == null)
  273. {
  274. throw new UserFriendlyException("图片数据有误");
  275. }
  276. string fileFilt = ".gif|.jpg|.jpeg|.png";
  277. if (fileFilt.IndexOf(ImageSuffix.ToLower(), StringComparison.Ordinal) <= -1)
  278. {
  279. throw new UserFriendlyException("请上传jpg、png、gif格式的图片");
  280. }
  281. savePath = AbsolutePath + "\\" + PatientRegisterId + "\\" + RegisterCheckId + "\\" + ImageName + ImageSuffix;
  282. hostPath = "/CheckPictureImg/" + PatientRegisterId + "/" + RegisterCheckId + "/" + ImageName + ImageSuffix;
  283. if (!Directory.Exists(AbsolutePath + "\\" + PatientRegisterId))
  284. {
  285. Directory.CreateDirectory(AbsolutePath + "\\" + PatientRegisterId);
  286. }
  287. if (!Directory.Exists(AbsolutePath + "\\" + PatientRegisterId + "\\" + RegisterCheckId))
  288. {
  289. Directory.CreateDirectory(AbsolutePath + "\\" + PatientRegisterId + "\\" + RegisterCheckId);
  290. }
  291. //图片后缀格式
  292. var suffix = savePath.Substring(savePath.LastIndexOf('.') + 1, savePath.Length - savePath.LastIndexOf('.') - 1).ToLower();
  293. var suffixName = suffix == "png" ? ImageFormat.Png :
  294. suffix == "jpg" || suffix == "jpeg" ? ImageFormat.Jpeg :
  295. suffix == "bmp" ? ImageFormat.Bmp :
  296. suffix == "gif" ? ImageFormat.Gif : ImageFormat.Jpeg;
  297. //这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示
  298. // var bmpNew = new Bitmap(bitmap);
  299. // bmpNew.Save(_hostingEnvironment.ContentRootPath + savePath, suffixName);
  300. using (FileStream fs = System.IO.File.Create(savePath))
  301. {
  302. file.CopyTo(fs);
  303. fs.Flush();
  304. }
  305. //var s1 = Directory.GetCurrentDirectory();
  306. //string saveurl = Path.Combine(s1, savePath);
  307. //bmpNew.Save(saveurl, suffixName);
  308. //bmpNew.Dispose();
  309. //bitmap.Dispose();
  310. ret = hostPath;
  311. }
  312. catch (Exception ex)
  313. {
  314. ret = "";
  315. }
  316. return ret;
  317. }
  318. /// <summary>
  319. /// 将Base64字符串转换为Image对象
  320. /// </summary>
  321. /// <param name="base64Str">base64字符串</param>
  322. /// <returns></returns>
  323. public static Bitmap Base64StrToImage(string base64Str)
  324. {
  325. Bitmap bitmap = null;
  326. byte[] arr = Convert.FromBase64String(base64Str);
  327. using (MemoryStream ms = new MemoryStream(arr))
  328. {
  329. Bitmap bmp = new Bitmap(ms);
  330. ms.Close();
  331. bitmap = bmp;
  332. }
  333. return bitmap;
  334. }
  335. public static string Base64ToImage(string base64String, string filePath)
  336. {
  337. if (string.IsNullOrWhiteSpace(filePath))
  338. {
  339. throw new UserFriendlyException("文件名不能为空");
  340. }
  341. if (string.IsNullOrWhiteSpace(base64String))
  342. {
  343. throw new UserFriendlyException("base64Str不能为空");
  344. }
  345. byte[] imageBytes = Convert.FromBase64String(base64String);
  346. using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
  347. {
  348. // Convert byte[] to Image
  349. using (Image image = Image.FromStream(ms))
  350. {
  351. // Save the image to file system
  352. string ImageSuffix = "";
  353. if (ImageFormat.Png.Guid == image.RawFormat.Guid)
  354. {
  355. ImageSuffix = ".png";
  356. }
  357. else if (ImageFormat.Gif.Guid == image.RawFormat.Guid)
  358. {
  359. ImageSuffix = ".gif";
  360. }
  361. else if (ImageFormat.Bmp.Guid == image.RawFormat.Guid)
  362. {
  363. ImageSuffix = ".bmp";
  364. }
  365. else
  366. {
  367. ImageSuffix = ".jpg";
  368. }
  369. image.Save(filePath + ImageSuffix);
  370. return filePath + ImageSuffix;
  371. }
  372. }
  373. }
  374. ///// <summary>
  375. ///// 图片转换为base64
  376. ///// </summary>
  377. ///// <param name="imgurl"></param>
  378. ///// <returns></returns>
  379. //public static string ImageToBase64Str(string imgurl)
  380. //{
  381. // string result = "";
  382. // try
  383. // {
  384. // using (FileStream fs = new FileStream(imgurl, FileMode.Open, FileAccess.Read))
  385. // {
  386. // byte[] byteArray = new byte[fs.Length];
  387. // fs.Read(byteArray, 0, byteArray.Length);
  388. // result = Convert.ToBase64String(byteArray);
  389. // }
  390. // }
  391. // catch(Exception ex)
  392. // {
  393. // result = "";
  394. // }
  395. // return result;
  396. //}
  397. ///// <summary>
  398. ///// 获取图片的Base64字符串
  399. ///// </summary>
  400. ///// <param name="imageUrl">imageUrl</param>
  401. ///// <returns></returns>
  402. //public static async Task<string> GetImageBase64StringAsync(string imageUrl)
  403. //{
  404. // // 创建 HttpClient 实例
  405. // using (var httpClient = new HttpClient())
  406. // {
  407. // // 下载图片
  408. // var response = await httpClient.GetAsync(imageUrl);
  409. // var contentStream = await response.Content.ReadAsStreamAsync();
  410. // // 将图片转换为 Base64 格式的字符串
  411. // using (var memoryStream = new MemoryStream())
  412. // {
  413. // await contentStream.CopyToAsync(memoryStream);
  414. // string base64String = Convert.ToBase64String(memoryStream.ToArray());
  415. // return base64String;
  416. // }
  417. // }
  418. //}
  419. /// <summary>
  420. /// 获取图片的Base64字符串
  421. /// </summary>
  422. /// <param name="imageUrl">imageUrl</param>
  423. /// <returns></returns>
  424. public static string GetImageBase64StringAsync(string imageUrl)
  425. {
  426. string result = "";
  427. try
  428. {
  429. using (FileStream fs = new FileStream(imageUrl, FileMode.Open, FileAccess.Read))
  430. {
  431. byte[] byteArray = new byte[fs.Length];
  432. fs.Read(byteArray, 0, byteArray.Length);
  433. result = Convert.ToBase64String(byteArray);
  434. }
  435. }
  436. catch
  437. {
  438. result = "";
  439. }
  440. if (!string.IsNullOrEmpty(result))
  441. result = "data:image/jpeg;base64," + result;
  442. return result;
  443. }
  444. public static string SavePacsFile(string absolutePath, string sourceFileFullName, string PatientRegisterId, string RegisterCheckId, string imageName)
  445. {
  446. var ret = "";
  447. string savePath = "";
  448. string hostPath = "";
  449. if (string.IsNullOrWhiteSpace(absolutePath))
  450. {
  451. throw new UserFriendlyException("absolutePath不能为空");
  452. }
  453. if (string.IsNullOrWhiteSpace(sourceFileFullName))
  454. {
  455. throw new UserFriendlyException("文件名不能为空");
  456. }
  457. if (!File.Exists(sourceFileFullName))
  458. {
  459. throw new UserFriendlyException("文件名不存在");
  460. }
  461. var ImageSuffix = Path.GetExtension(sourceFileFullName);
  462. if (string.IsNullOrWhiteSpace(imageName))
  463. {
  464. throw new UserFriendlyException("图片名不能为空");
  465. }
  466. if (string.IsNullOrWhiteSpace(ImageSuffix))
  467. {
  468. throw new UserFriendlyException("图片数据后缀名不能为空");
  469. }
  470. if (string.IsNullOrWhiteSpace(PatientRegisterId))
  471. {
  472. throw new UserFriendlyException("PatientRegisterId不能为空");
  473. }
  474. if (string.IsNullOrWhiteSpace(RegisterCheckId))
  475. {
  476. throw new UserFriendlyException("RegisterCheckId不能为空");
  477. }
  478. string fileFilt = ".gif|.jpg|.jpeg|.png";
  479. if (fileFilt.IndexOf(ImageSuffix.ToLower(), StringComparison.Ordinal) <= -1)
  480. {
  481. throw new UserFriendlyException("请上传jpg、png、gif格式的图片");
  482. }
  483. savePath = absolutePath + "\\" + PatientRegisterId + "\\" + RegisterCheckId + "\\" + imageName + ImageSuffix;
  484. hostPath = "/CheckPictureImg/" + PatientRegisterId + "/" + RegisterCheckId + "/" + imageName + ImageSuffix;
  485. if (!Directory.Exists(absolutePath + "\\" + PatientRegisterId))
  486. {
  487. Directory.CreateDirectory(absolutePath + "\\" + PatientRegisterId);
  488. }
  489. if (!Directory.Exists(absolutePath + "\\" + PatientRegisterId + "\\" + RegisterCheckId))
  490. {
  491. Directory.CreateDirectory(absolutePath + "\\" + PatientRegisterId + "\\" + RegisterCheckId);
  492. }
  493. File.Copy(sourceFileFullName, savePath, true);//true允许覆盖
  494. ret = hostPath;
  495. return ret;
  496. }
  497. }
  498. }