[TestMethod()]
public void JpgToDicomTest()
{
// 加载JPG文件
var srcPath = "C:\\Users\\Administrator\\Pictures\\Saved Pictures\\11.jpg";
var dstPath = "C:\\Users\\Administrator\\Pictures\\Saved Pictures\\11.dcm";
var pixels = File.ReadAllBytes(srcPath);
using var buffer = new MemoryStream(pixels);
using var bitmap = System.Drawing.Image.FromStream(buffer);
// 创建DICOM对象
var dataset = new DicomDataset(DicomTransferSyntax.JPEGProcess1)
{
{ DicomTag.SOPClassUID, "1.2.840.10008.5.1.4.1.1.2" },
{ DicomTag.SOPInstanceUID, DicomUID.Generate() },
{ DicomTag.StudyInstanceUID, "1" },
{ DicomTag.SeriesInstanceUID, "1" },
{ DicomTag.PatientID, "1" },
{ DicomTag.PatientName, "Test" },
{ DicomTag.Modality, "ECG" },
{ DicomTag.Rows, (ushort)bitmap.Height },
{ DicomTag.Columns, (ushort)bitmap.Width },
{ DicomTag.BitsAllocated, (ushort)8 },
{ DicomTag.PhotometricInterpretation, PhotometricInterpretation.YbrFull422.Value }
};
DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
pixelData.SamplesPerPixel = 3;
pixelData.BitsStored = 8;
pixelData.HighBit = 7;
pixelData.PixelRepresentation = 0;
pixelData.PlanarConfiguration = 0;
//pixelData.PhotometricInterpretation = PhotometricInterpretation.YbrFull422;//有一个就行
//以下就是重点部分
var fragment = new DicomOtherByteFragment(DicomTag.PixelData);
fragment.Fragments.Add(EmptyBuffer.Value);
fragment.Fragments.Add(EvenLengthBuffer.Create(new MemoryByteBuffer(pixels)));
pixelData.AddFrame(new CompositeByteBuffer(fragment));
var file = new DicomFile(dataset);
file.Save(dstPath);
}
C# 中使用fo-dicom库添加jpg原生图片
于 2023-07-13 10:39:27 首次发布