public static void ImportImage(string file){Bitmap bitmap = new Bitmap(file);//bitmap = GetValidImage(bitmap);byte[] pixels = GetPixels(bitmap);MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);DicomDataset dataset = new DicomDataset();//FillDataset(dataset);dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);dataset.Add(DicomTag.Rows, (ushort)bitmap.Height);dataset.Add(DicomTag.Columns, (ushort)bitmap.Width);dataset.Add(DicomTag.BitsAllocated, (ushort)8);dataset.Add(DicomTag.SOPClassUID, "1.2.840.10008.5.1.4.1.1.2");dataset.Add(DicomTag.SOPInstanceUID, "1.2.840.10008.5.1.4.1.1.2.20181120090837121314");DicomPixelData pixelData = DicomPixelData.Create(dataset, true);pixelData.BitsStored = 8;//pixelData.BitsAllocated = 8;pixelData.SamplesPerPixel = 3;pixelData.HighBit = 7;pixelData.PixelRepresentation = 0;pixelData.PlanarConfiguration = 0;pixelData.AddFrame(buffer);DicomFile dicomfile = new DicomFile(dataset);dicomfile.Save(@"e:\dicomfile.dcm");SaveDicomFile2(pixels, bitmap.Height, bitmap.Width);}
其中傳入參數就是BMP/JPG/PNG圖像的路徑。這樣你只需要一個入口程序(比如控制臺的Main函數)就可以將一個圖片轉為DICOM還是彩色的。 這里只設置了DICOM必須的幾個字段,這幾個字段的意義可以參考官方標準說明。部分說明如下: DICOM文件格式: 1)Samples Per Pixel:
標簽為(0028,0002),具體的介紹在DICOM3.0標準第3部分的附錄C7.6.3.1。含義表示【the number of separate planes in this image】,就像PhotoShop中的通道,每個通道表示一種顏色(除了RGB三個通道以外,也會存在第四個通透性通道)。對于灰度圖像(monochrome或gray)和顏色表圖像(palette,就是BMP格式中介紹的有調色板的BMP文件),該標簽值為1,RGB圖像或其他色彩模式圖像,該標簽值為3。本實例中使用的BMP圖像是RGB格式的,因此SamplePerPixel=3,起初的文件格式錯誤就是由于該字段設置為1所致。