using System; using System.Collections.Generic; using System.Drawing.Imaging; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using Dicom; using Dicom.Imaging; using Dicom.IO.Buffer; using System.IO; using System.Collections; namespace videoGather { public class Imaganddcm { /// /// 存放转换后的dcm文件名称 /// public static ArrayList Array=new ArrayList(); /// /// 转换为Dicom格式 /// /// 患者ID /// 影像唯一号 /// 访问号 /// 患者姓名 /// jpg图像 /// dicom输出路径 /// public static bool ConvertToDicomImages(string patId, string pisId, string textId, string patName, Bitmap inputImagePath, string outputDicomPath) { // 读取图像 // 创建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)inputImagePath.Height }, { DicomTag.Columns, (ushort)inputImagePath.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(inputImagePath); pixelData.AddFrame(new MemoryByteBuffer(imageData)); // 创建 DICOM 文件 var dicomFile = new DicomFile(dicomDataset); string guid= Guid.NewGuid().ToString("D"); // 保存 DICOM 文件 dicomFile.Save(outputDicomPath+"\\"+ guid+".dcm"); Array.Add(outputDicomPath + "\\" + guid + ".dcm"); return File.Exists(outputDicomPath+ "\\" + guid + ".dcm"); } private static byte[] GetImageData(Bitmap image) { // 将图像数据转换为DICOM格式 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++) { // Copy the data from the bitmap into the array System.Runtime.InteropServices.Marshal.Copy(ptr + (y * bitmapData.Stride), pixels, y * actualDataWidth, actualDataWidth); } image.UnlockBits(bitmapData); // Swap the Red and Blue channels to convert from RGB to BGR for (int i = 0; i < byteCount; i += 3) { byte temp = pixels[i]; pixels[i] = pixels[i + 2]; pixels[i + 2] = temp; } return pixels; } public static bool ImportImage(string patId, string pisId, string textId, string patName, Bitmap inputImagePath, string outputDicomPath) { //bitmap = GetValidImage(bitmap); byte[] pixels = GetPixels(inputImagePath); MemoryByteBuffer buffer = new MemoryByteBuffer(pixels); DicomDataset dataset = new DicomDataset(); //FillDataset(dataset); dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value); dataset.Add(DicomTag.Rows, (ushort)inputImagePath.Height); dataset.Add(DicomTag.Columns, (ushort)inputImagePath.Width); dataset.Add(DicomTag.BitsAllocated, (ushort)8); dataset.Add(DicomTag.SOPClassUID, DicomUIDGenerator.GenerateDerivedFromUUID().UID); dataset.Add(DicomTag.SOPInstanceUID, DicomUIDGenerator.GenerateDerivedFromUUID().UID); dataset.Add(DicomTag.PatientID, patId); dataset.Add(DicomTag.PatientName, patName); dataset.Add(DicomTag.StudyInstanceUID, pisId); 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); string guid = Guid.NewGuid().ToString("D"); Array.Add(outputDicomPath + "\\" + guid + ".dcm"); dicomfile.Save(outputDicomPath + "\\" + guid + ".dcm"); return File.Exists(outputDicomPath + "\\" + guid + ".dcm"); } public static byte[] GetPixels(Bitmap bitmap) { byte[] bytes = new byte[bitmap.Width * bitmap.Height * 3]; int wide = bitmap.Width; int i = 0; int height = bitmap.Height; for (int y = 0; y < height; y++) { for (int x = 0; x < wide; x++) { var srcColor = bitmap.GetPixel(x, y); //bytes[i] = (byte)(srcColor.R * .299 + srcColor.G * .587 + srcColor.B * .114); bytes[i] = srcColor.R; i++; bytes[i] = srcColor.G; i++; bytes[i] = srcColor.B; i++; } } return bytes; } } }