安装Nuget包:fo-dicom 5.1.2版本
代码如下:
/// <summary>
/// 转换为Dicom格式
/// </summary>
/// <param name="patId">患者ID</param>
/// <param name="pisId">影像唯一号</param>
/// <param name="textId">访问号</param>
/// <param name="patName">患者姓名</param>
/// <param name="inputImagePath">jpg图像路径</param>
/// <param name="outputDicomPath">dicom输出路径</param>
/// <returns></returns>
public static bool ConvertToDicomImages(string patId,string pisId,string textId,string patName, string inputImagePath, string outputDicomPath)
{
// 读取图像
using (var originalImage = new Bitmap(inputImagePath))
{
// 创建DICOM数据集
var dicomDataset = new DicomDataset
{
{ DicomTag.PatientID, patId },
{ DicomTag.PatientName, patName },
{ DicomTag.StudyInstanceUID, pisId },//StudyUid
{ DicomTag.SeriesInstanceUID, DicomUIDGenerator.GenerateDerivedFromUUID().UID },
{ DicomTag.SOPInstanceUID, DicomUIDGenerator.GenerateDerivedFromUUID().UID },
{ DicomTag.AccessionNumber,textId },//访问号
{ DicomTag.SOPClassUID, DicomUID.SecondaryCaptureImageStorage },
{ DicomTag.PhotometricInterpretation, "RGB" },
{ DicomTag.Rows, (ushort)originalImage.Height },
{ DicomTag.Columns, (ushort)originalImage.Width },
{ DicomTag.SamplesPerPixel, (ushort)3 },
{ DicomTag.BitsAllocated, (ushort)8 },
{ DicomTag.BitsStored, (ushort)8 },
{ DicomTag.HighBit, (ushort)7 }
};
// 创建像素数据
var pixelData = DicomPixelData.Create(dicomDataset, true);
byte[] imageData = GetImageData(originalImage);
pixelData.AddFrame(new MemoryByteBuffer(imageData));
// 创建 DICOM 文件
var dicomFile = new DicomFile(dicomDataset);
// 保存 DICOM 文件
dicomFile.Save(outputDicomPath);
return File.Exists(outputDicomPath);
}
}
private static byte[] GetImageData(Bitmap image)
{
BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
int actualDataWidth = image.Width * 3;
int byteCount = actualDataWidth * image.Height;
byte[] pixels = new byte[byteCount];
IntPtr ptr = bitmapData.Scan0;
for (int y = 0; y < image.Height; y++)
{
System.Runtime.InteropServices.Marshal.Copy(ptr + (y * bitmapData.Stride), pixels, y * actualDataWidth, actualDataWidth);
}
image.UnlockBits(bitmapData);
return pixels;
}
标签:bitmapData,string,C#,image,ushort,jpg,new,格式,DicomTag From: https://www.cnblogs.com/zengbingqian/p/18290146