c#写dicom文件

 /// <summary>
        /// Writes a dicom file from an array of image data and the width and height
        /// </summary>
        /// <param name="greybytes">byte array of image data, this example uses 2 bytes for each greyscale pixel</param>
        /// <param name="imgwidth">width in pixels</param>
        /// <param name="imgheight">height in pixels</param>
        public void MakeGreyDicom(byte[] greybytes, ushort imgwidth, ushort imgheight)
        {
            DcmUID studyUid = DcmUID.Generate();
            DcmUID seriesUid = DcmUID.Generate(studyUid, 1);
            DcmUID instUid = DcmUID.Generate(seriesUid, 1);
            DcmDataset data = new DcmDataset(DcmTS.ImplicitVRLittleEndian);
            data.AddElementWithValue(DcmTags.SOPClassUID, DcmUIDs.ComputedRadiographyImageStorage);
            //data.AddElementWithValue(DcmTags.SOPClassUID, DcmUIDs.SecondaryCapture); 
            data.AddElementWithValue(DcmTags.StudyInstanceUID, studyUid);
            data.AddElementWithValue(DcmTags.SeriesInstanceUID, seriesUid);
            data.AddElementWithValue(DcmTags.SOPInstanceUID, instUid);
 
            //data.AddElementWithValue(DcmTags.MediaStorageSOPClassUID, DcmUIDs.ImplicitVRLittleEndian);
            //data.AddElementWithValueString(DcmTags.MediaStorageSOPClassUID, DcmUIDs.ComputedRadiographyImageStorage.ToString());
 
            //type 2 attributes
            data.AddElementWithValueString(DcmTags.PatientID, "12345");
            data.AddElementWithValueString(DcmTags.PatientsName, "Doe^John");
            data.AddElementWithValueString(DcmTags.PatientsBirthDate, "00000000");
            data.AddElementWithValueString(DcmTags.PatientsSex, "M");
            data.AddElementWithValue(DcmTags.StudyDate, DateTime.Now);
            data.AddElementWithValue(DcmTags.StudyTime, DateTime.Now);
            data.AddElementWithValueString(DcmTags.AccessionNumber, "");
            data.AddElementWithValueString(DcmTags.ReferringPhysiciansName, "");
            data.AddElementWithValueString(DcmTags.StudyID, "1");
            data.AddElementWithValueString(DcmTags.SeriesNumber, "1");
            data.AddElementWithValueString(DcmTags.ModalitiesInStudy, "CR");
            data.AddElementWithValueString(DcmTags.Modality, "CR");
            data.AddElementWithValueString(DcmTags.NumberOfStudyRelatedInstances, "1");
            data.AddElementWithValueString(DcmTags.NumberOfStudyRelatedSeries, "1");
            data.AddElementWithValueString(DcmTags.NumberOfSeriesRelatedInstances, "1");
            data.AddElementWithValueString(DcmTags.PatientOrientation, "F/A");
            data.AddElementWithValueString(DcmTags.ImageLaterality, "U");
 
            ushort bitdepth = 2;
 
            DcmPixelData pixelData = new DcmPixelData(DcmTS.ImplicitVRLittleEndian);
 
            pixelData.ImageWidth = imgwidth;
            pixelData.ImageHeight = imgheight;
            pixelData.SamplesPerPixel = 1;
 
            pixelData.HighBit = 11;
            pixelData.BitsStored = 12;
            pixelData.BitsAllocated = 16;
 
            pixelData.PhotometricInterpretation = "MONOCHROME1";//2 byte gray?
            pixelData.PixelDataElement = DcmElement.Create(DcmTags.PixelData, DcmVR.OB); //OB: Other Byte, OW: Other Word
 
            //pixelData.AddFrame(bmpBytes);
            pixelData.AddFrame(greybytes);
 
            pixelData.UpdateDataset(data);
            DicomFileFormat ff = new DicomFileFormat(data);
            string fileout = Path.Combine(Directory.GetCurrentDirectory(), "greyimg.dcm");
            ff.Save(fileout, DicomWriteOptions.Default);
            ff = null;
        }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值