From 1ea73826141ed396bbcdf3d0971303075b935999 Mon Sep 17 00:00:00 2001 From: "DESKTOP-G961P6V\\Zhh" <839860190@qq.com> Date: Wed, 15 May 2024 21:38:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=85=A5=E7=BB=93=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImportPacsResultAppService.cs | 4 +- src/Shentun.Peis.Domain/ImageHelper.cs | 147 ++++++++++++++++-- .../ImportPacsResultAppServiceTest.cs | 36 ++--- 3 files changed, 158 insertions(+), 29 deletions(-) diff --git a/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs b/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs index 9161f51..677e921 100644 --- a/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs +++ b/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs @@ -247,7 +247,7 @@ namespace Shentun.Peis.ImportPacsResults //0-图片,1-PDF if (file.FileFormat == "0") { - pictureUrl = ImageHelper.Base64StrToImageInAbsolutePath(absolutePath, fileName, file.FileBase64, firstEntity.patientRegister.Id.ToString(), + pictureUrl = ImageHelper.Base64ToImageInAbsolutePath(absolutePath, fileName, file.FileBase64, firstEntity.patientRegister.Id.ToString(), firstEntity.registerCheck.Id.ToString()); } @@ -286,7 +286,7 @@ namespace Shentun.Peis.ImportPacsResults if (string.IsNullOrWhiteSpace(pictureUrl)) { - throw new UserFriendlyException("pictureUrl是空值"); + throw new UserFriendlyException("pictureUrl不能是空值"); } var registerCheckPicture = new RegisterCheckPicture { diff --git a/src/Shentun.Peis.Domain/ImageHelper.cs b/src/Shentun.Peis.Domain/ImageHelper.cs index c06d921..f1eec0d 100644 --- a/src/Shentun.Peis.Domain/ImageHelper.cs +++ b/src/Shentun.Peis.Domain/ImageHelper.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; +//using System.DrawingCore; using System.IO; using System.Linq; using System.Net; @@ -274,7 +275,88 @@ namespace Shentun.Peis } + public static string Base64ToImageInAbsolutePath(string AbsolutePath, string fileName, string base64Str, string PatientRegisterId, string RegisterCheckId) + { + var ret = ""; + + try + { + + base64Str = base64Str.Substring(base64Str.IndexOf(",") + 1); + + string savePath = ""; + string hostPath = ""; + var bitmap = Base64StrToImage(base64Str); + + if (bitmap != null) + { + string ImageSuffix = ""; + if (ImageFormat.Png.Guid == bitmap.RawFormat.Guid) + { + ImageSuffix = ".png"; + } + else if (ImageFormat.Gif.Guid == bitmap.RawFormat.Guid) + { + ImageSuffix = ".gif"; + } + else if (ImageFormat.Bmp.Guid == bitmap.RawFormat.Guid) + { + ImageSuffix = ".bmp"; + } + else + { + ImageSuffix = ".jpg"; + } + + string savaDirectory = $"{AbsolutePath}\\pacs\\{DateTime.Now.Year}\\{DateTime.Now.Month}\\{DateTime.Now.Day}\\{PatientRegisterId}\\{RegisterCheckId}"; + + savePath = $"{savaDirectory}\\{fileName + ImageSuffix}"; + hostPath = $"/CheckPictureImg/pacs/{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}/{PatientRegisterId}/{RegisterCheckId}/{fileName + ImageSuffix}"; + + + if (!Directory.Exists(savaDirectory)) + { + Directory.CreateDirectory(savaDirectory); + } + + + + //图片后缀格式 + var suffix = savePath.Substring(savePath.LastIndexOf('.') + 1, savePath.Length - savePath.LastIndexOf('.') - 1).ToLower(); + var suffixName = suffix == "png" ? ImageFormat.Png : + suffix == "jpg" || suffix == "jpeg" ? ImageFormat.Jpeg : + suffix == "bmp" ? ImageFormat.Bmp : + suffix == "gif" ? ImageFormat.Gif : ImageFormat.Jpeg; + + + + //这里复制一份对图像进行保存,否则会出现“GDI+ 中发生一般性错误”的错误提示 + var bmpNew = new Bitmap(bitmap); + // bmpNew.Save(_hostingEnvironment.ContentRootPath + savePath, suffixName); + + var s1 = Directory.GetCurrentDirectory(); + string saveurl = Path.Combine(s1, savePath); + + bmpNew.Save(saveurl, suffixName); + bmpNew.Dispose(); + + bitmap.Dispose(); + + ret = hostPath; + } + else + { + ret = ""; + } + } + catch (Exception ex) + { + ret = ""; + } + + return ret; + } /// @@ -388,21 +470,48 @@ namespace Shentun.Peis { Bitmap bitmap = null; - try + + byte[] arr = Convert.FromBase64String(base64Str); + using (MemoryStream ms = new MemoryStream(arr)) { - byte[] arr = Convert.FromBase64String(base64Str); - MemoryStream ms = new MemoryStream(arr); + Bitmap bmp = new Bitmap(ms); ms.Close(); bitmap = bmp; } - catch (Exception ex) - { - } - return bitmap; } + public static void ConvertFromBase64(string base64String, string filePath) + { + byte[] imageBytes = Convert.FromBase64String(base64String); + using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) + { + // Convert byte[] to Image + using (Image image = Image.FromStream(ms)) + { + // Save the image to file system + string ImageSuffix = ""; + if (ImageFormat.Png.Guid == image.RawFormat.Guid) + { + ImageSuffix = ".png"; + } + else if (ImageFormat.Gif.Guid == image.RawFormat.Guid) + { + ImageSuffix = ".gif"; + } + else if (ImageFormat.Bmp.Guid == image.RawFormat.Guid) + { + ImageSuffix = ".bmp"; + } + else + { + ImageSuffix = ".jpg"; + } + image.Save(filePath); + } + } + } ///// ///// 图片转换为base64 @@ -488,6 +597,12 @@ namespace Shentun.Peis string savePath = ""; string hostPath = ""; + + if (string.IsNullOrWhiteSpace(absolutePath)) + { + throw new UserFriendlyException("absolutePath不能为空"); + } + if (string.IsNullOrWhiteSpace(sourceFileFullName)) { throw new UserFriendlyException("文件名不能为空"); @@ -498,14 +613,28 @@ namespace Shentun.Peis throw new UserFriendlyException("文件名不存在"); } + var ImageSuffix = Path.GetExtension(sourceFileFullName); + if (string.IsNullOrWhiteSpace(imageName)) + { + throw new UserFriendlyException("图片名不能为空"); + } - if (ImageSuffix == null) + if (string.IsNullOrWhiteSpace(ImageSuffix)) { - throw new UserFriendlyException("图片数据有误"); + throw new UserFriendlyException("图片数据后缀名不能为空"); } + if (string.IsNullOrWhiteSpace(PatientRegisterId)) + { + throw new UserFriendlyException("PatientRegisterId不能为空"); + } + + if (string.IsNullOrWhiteSpace(RegisterCheckId)) + { + throw new UserFriendlyException("RegisterCheckId不能为空"); + } string fileFilt = ".gif|.jpg|.jpeg|.png"; if (fileFilt.IndexOf(ImageSuffix.ToLower(), StringComparison.Ordinal) <= -1) diff --git a/test/Shentun.Peis.Application.Tests/ImportPacsResultAppServiceTest.cs b/test/Shentun.Peis.Application.Tests/ImportPacsResultAppServiceTest.cs index 1baf36f..8cd4bdb 100644 --- a/test/Shentun.Peis.Application.Tests/ImportPacsResultAppServiceTest.cs +++ b/test/Shentun.Peis.Application.Tests/ImportPacsResultAppServiceTest.cs @@ -44,31 +44,31 @@ namespace Shentun.Peis CheckDoctorName = "张医生", Files = new List() { - new CreateImportPacsResultPictureDto() - { - IsPrint = 'Y', - FileName = Guid.NewGuid().ToString(), - FileFormat = "0", - FileTransMode = "0", - FileBase64 = Shentun.Utilities.FileHelper.ToBase64("E:\\Whitedolphins\\prog20220722\\pic\\login.png") - }, - new CreateImportPacsResultPictureDto() - { - IsPrint = 'Y', - FileFormat = "0", - FileTransMode = "0", - FileName = Guid.NewGuid().ToString(), - FileBase64 = Shentun.Utilities.FileHelper.ToBase64("E:\\Whitedolphins\\prog20220722\\pic\\首页背景图.jpg") - }, + //new CreateImportPacsResultPictureDto() + //{ + // IsPrint = 'Y', + // FileName = Guid.NewGuid().ToString(), + // FileFormat = "0", + // FileTransMode = "0", + // FileBase64 = Shentun.Utilities.FileHelper.ToBase64("E:\\Whitedolphins\\prog20220722\\pic\\login.png") + //}, + // new CreateImportPacsResultPictureDto() + //{ + // IsPrint = 'Y', + // FileFormat = "0", + // FileTransMode = "0", + // FileName = Guid.NewGuid().ToString(), + // FileBase64 = Shentun.Utilities.FileHelper.ToBase64("E:\\Whitedolphins\\prog20220722\\pic\\首页背景图.jpg") + //}, new CreateImportPacsResultPictureDto() { IsPrint = 'Y', FileFormat = "1", - FileTransMode = "1", + FileTransMode = "0", FileName = "http://10.1.13.18:8380//image/66389af146ac4b27f4da93ff/risPdf/3135.pdf", FileUrl = "http://10.1.13.18:8380//image/66389af146ac4b27f4da93ff/risPdf/3135.pdf", - // FileBase64 = Shentun.Utilities.FileHelper.ToBase64("http://10.1.13.18:8380//image/66389af146ac4b27f4da93ff/risPdf/3135.pdf") + FileBase64 = Shentun.Utilities.FileHelper.ToBase64("http://10.1.13.18:8380//image/66389af146ac4b27f4da93ff/risPdf/3135.pdf") }, } };