From 434cdf8529979bdf001e1e531289b973c604dbf4 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Mon, 11 May 2026 10:49:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E7=AB=AF=E4=B8=8B=E8=BD=BD=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=88=9D=E6=AD=A5=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Common/TrialImageDownloadService.cs | 64 +++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/IRaCIS.Core.Application/Service/Common/TrialImageDownloadService.cs b/IRaCIS.Core.Application/Service/Common/TrialImageDownloadService.cs index 26b3e73a8..a23703c16 100644 --- a/IRaCIS.Core.Application/Service/Common/TrialImageDownloadService.cs +++ b/IRaCIS.Core.Application/Service/Common/TrialImageDownloadService.cs @@ -17,6 +17,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using MiniExcelLibs; +using NPOI.Util; using SharpCompress.Common; using System; using System.Collections.Generic; @@ -49,7 +50,51 @@ namespace IRaCIS.Core.Application.Service + private static async Task TryWriteMergedDicomAsync(Func> sourceFactory, Stream output) + { + try + { + await using var source = await sourceFactory(); + // 如果你是从 stream 打开 + var dicomFile = await DicomFile.OpenAsync(source); + //获取像素是否为封装形式 + var syntax = dicomFile.Dataset.InternalTransferSyntax; + + //对于封装像素的文件做转换 + if (syntax.IsEncapsulated) + { + // 获取 Pixel Data 标签 + var pixelData = DicomPixelData.Create(dicomFile.Dataset); + + // 创建一个新的片段序列 + var newFragments = new DicomOtherByteFragment(DicomTag.PixelData); + // 获取每帧数据并封装为单独的片段 + for (int n = 0; n < pixelData.NumberOfFrames; n++) + { + var frameData = pixelData.GetFrame(n); + newFragments.Fragments.Add(new MemoryByteBuffer(frameData.Data)); + } + + var frag = dicomFile.Dataset.GetDicomItem(DicomTag.PixelData); + + var originOffsetTable = frag?.OffsetTable; + + newFragments.OffsetTable.AddRange(originOffsetTable?.ToArray()); + // 替换原有的片段序列 + dicomFile.Dataset.AddOrUpdate(newFragments); + } + + await dicomFile.SaveAsync(output); + return true; + } + catch (Exception ex) + { + // 只记录,不传播 + Log.Logger.Warning($"TryWriteMergedDicomAsync failed: {ex.Message}"); + return false; + } + } /// @@ -88,7 +133,9 @@ namespace IRaCIS.Core.Application.Service InstancePathList = z.DicomInstanceList.Where(t => t.IsReading).Select(k => new { - k.Path + k.Path, + k.IsEncapsulated, + k.NumberOfFrames, }).ToList() }) @@ -171,9 +218,18 @@ namespace IRaCIS.Core.Application.Service // 复制文件到相应的文件夹 string destinationPath = Path.Combine(studyDicomFolderPath, Path.GetFileName(instanceInfo.Path)); - - //加入到下载任务里 - downloadJobs.Add(() => _oSSService.DownLoadFromOSSAsync(instanceInfo.Path, destinationPath)); + await using var output = File.Create(destinationPath); + if (instanceInfo.IsEncapsulated) + { + //加入到下载任务里 + downloadJobs.Add(() => TryWriteMergedDicomAsync(() => _oSSService.GetStreamFromOSSAsync(instanceInfo.Path), output)); + } + else + { + //加入到下载任务里 + downloadJobs.Add(() => _oSSService.DownLoadFromOSSAsync(instanceInfo.Path, destinationPath)); + } + //下载到当前目录 //await _oSSService.DownLoadFromOSSAsync(instanceInfo.Path, destinationPath);