From 42fb774d5bbba4f9808c0dd6be5a0f32b65da55d Mon Sep 17 00:00:00 2001 From: "DESKTOP-G961P6V\\Zhh" <839860190@qq.com> Date: Wed, 15 May 2024 22:29:22 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=93=E6=9E=9C=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ImportPacsResultPlugInsGem.cs | 8 +- .../ImportPacsResultAppService.cs | 2 +- src/Shentun.Peis.Domain/ImageHelper.cs | 114 +++++++----------- 3 files changed, 48 insertions(+), 76 deletions(-) diff --git a/ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportPacsResultPlugInsGem.cs b/ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportPacsResultPlugInsGem.cs index e1cbe75..ee7fbec 100644 --- a/ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportPacsResultPlugInsGem.cs +++ b/ThirdPlugIns/Shentun.Peis.PlugIns.Gem/ImportPacsResultPlugInsGem.cs @@ -73,11 +73,11 @@ namespace Shentun.Peis.PlugIns.Gem new CreateImportPacsResultPictureDto() { IsPrint = 'Y', - FileTransMode = "0",//0-json,1-url + FileTransMode = "1",//0-json,1-url FileName = firstData.reportUrl, - FileFormat = "0",//0-图片,1-pdf - FileUrl = Guid.NewGuid().ToString(), - FileBase64 = Shentun.Utilities.FileHelper.ToBase64(firstData.reportUrl) + FileFormat = "1",//0-图片,1-pdf + FileUrl = firstData.reportUrl, + //FileBase64 = Shentun.Utilities.FileHelper.ToBase64(firstData.reportUrl) }, } diff --git a/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs b/src/Shentun.Peis.Application/ImportPacsResults/ImportPacsResultAppService.cs index 677e921..f2c6b00 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.Base64ToImageInAbsolutePath(absolutePath, fileName, file.FileBase64, firstEntity.patientRegister.Id.ToString(), + pictureUrl = ImageHelper.Base64ToImageUseAbsolutePath(absolutePath, fileName, file.FileBase64, firstEntity.patientRegister.Id.ToString(), firstEntity.registerCheck.Id.ToString()); } diff --git a/src/Shentun.Peis.Domain/ImageHelper.cs b/src/Shentun.Peis.Domain/ImageHelper.cs index f1eec0d..e9c36e9 100644 --- a/src/Shentun.Peis.Domain/ImageHelper.cs +++ b/src/Shentun.Peis.Domain/ImageHelper.cs @@ -275,86 +275,46 @@ namespace Shentun.Peis } - public static string Base64ToImageInAbsolutePath(string AbsolutePath, string fileName, string base64Str, string PatientRegisterId, string RegisterCheckId) + public static string Base64ToImageUseAbsolutePath(string absolutePath, string fileName, string base64Str, string patientRegisterId, string registerCheckId) { var ret = ""; - try + if (string.IsNullOrWhiteSpace(absolutePath)) { + throw new UserFriendlyException("absolutePath不能为空"); + } - 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); + if (string.IsNullOrWhiteSpace(fileName)) + { + throw new UserFriendlyException("文件名不能为空"); + } - bmpNew.Save(saveurl, suffixName); - bmpNew.Dispose(); + if (string.IsNullOrWhiteSpace(base64Str)) + { + throw new UserFriendlyException("base64Str不能为空"); + } - bitmap.Dispose(); + if (string.IsNullOrWhiteSpace(patientRegisterId)) + { + throw new UserFriendlyException("PatientRegisterId不能为空"); + } - ret = hostPath; - } - else - { - ret = ""; - } + if (string.IsNullOrWhiteSpace(registerCheckId)) + { + throw new UserFriendlyException("RegisterCheckId不能为空"); } - catch (Exception ex) + + string savaDirectory = $"{absolutePath}\\pacs\\{DateTime.Now.Year}\\{DateTime.Now.Month}\\{DateTime.Now.Day}\\{patientRegisterId}\\{registerCheckId}"; + if (!Directory.Exists(savaDirectory)) { - ret = ""; + Directory.CreateDirectory(savaDirectory); } + var savePath = $"{savaDirectory}\\{fileName}"; + var fullFileName = Base64ToImage(base64Str, savePath); + var ImageSuffix = Path.GetExtension(fullFileName); + var hostPath = $"/CheckPictureImg/pacs/{DateTime.Now.Year}/{DateTime.Now.Month}/{DateTime.Now.Day}/{patientRegisterId}/{registerCheckId}/{fileName + ImageSuffix}"; + ret = hostPath; return ret; } @@ -474,7 +434,7 @@ namespace Shentun.Peis byte[] arr = Convert.FromBase64String(base64Str); using (MemoryStream ms = new MemoryStream(arr)) { - + Bitmap bmp = new Bitmap(ms); ms.Close(); bitmap = bmp; @@ -482,8 +442,19 @@ namespace Shentun.Peis return bitmap; } - public static void ConvertFromBase64(string base64String, string filePath) + public static string Base64ToImage(string base64String, string filePath) { + if (string.IsNullOrWhiteSpace(filePath)) + { + throw new UserFriendlyException("文件名不能为空"); + } + + if (string.IsNullOrWhiteSpace(base64String)) + { + throw new UserFriendlyException("base64Str不能为空"); + } + + byte[] imageBytes = Convert.FromBase64String(base64String); using (MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length)) { @@ -508,7 +479,8 @@ namespace Shentun.Peis { ImageSuffix = ".jpg"; } - image.Save(filePath); + image.Save(filePath + ImageSuffix); + return filePath + ImageSuffix; } } }