置百丈玄冰而崩裂,掷须臾池水而漂摇。

RIS中PDF转换DICOM文件(dicomcs )

RIS系统 强强

RIS调用dicomcs 根据传入pdf路径 生成DCM文件的代码

说明:(非DCM图像,只是存储了)  


生成DICOM文件的基本代码


FileDicom deal = new FileDicom();
DicomFileInfo info = new DicomFileInfo();
info.AccessionNum = modalityid; //检查号
info.FilePath = file; //pdf路径
info.InsNum = "9";
info.PatientAge = CurApply.PAT_AGE; //年龄
info.PatientBirthDate = CurPatient.BIRTHDATE == null ? "" : ((DateTime)CurPatient.BIRTHDATE).ToString("yyyy-MM-dd"); //出生年月
info.PatientID = patid; //影像号
info.PatientName = CurPatient.PATNAME; //姓名
info.PatientSex = CurPatient.SEX; //性别
info.SeriesNum = "90001"; 
info.SeriesUID = "2.2.280.8888." + modalityid; 
info.SopUID = "2.2.280.9999." + modalityid;
info.StudyDate = CurApply.ACCESSION_DATE.ToString("yyyyMMdd"); //检查日期
info.StudyID = modalityid; // 检查号
info.StudyInsUID = "2.2.280.7777." + modalityid;
info.StudyTime = CurApply.ACCESSION_DATE.ToString("hh:mm:ss"); //检查时间
info.FileName = Path.GetFileNameWithoutExtension(file); //文件名称
info.FileType = "pdf"; //类型
info.Modality = "SR"; //l类型
string outFile = string.Format(@"{0}\{1}_{2}.dcm", tempDicomPath, modalityid, info.FileName); //输出路径               
deal.GenerateFileDicom(info, outFile);    //生成文件

FileDicom 类,生成文件:

public class FileDicom
    {
        public const uint FILETAG = 0x7f010001;
        public const uint FILETYPETAG = 0x00810001;
        public const uint FILENAMETAG = 0x00810002;

        public void GenerateFileDicom(DicomFileInfo info, string outPath)
        {
            if (info == null) return;
            string dcmPath = string.Empty;

            DcmDataset ds = null;
            try
            {
                ds = this.GenerateStanderDcmDataset(info);
            }
            catch (Exception ex)
            {
                throw new Exception("构造报告dicom文件时出错:" + ex.Message);
            }

            try
            {
                ds.DcmSaveToFile(outPath, ds, UIDs.ExplicitVRLittleEndian);
            }
            catch (Exception ex)
            {
                throw new Exception("构造报告dicom文件时出错:" + ex.Message);
            }
        }

        public string ParseFile(string dcmFile, string outDirectory, string fileType = "")
        {
            string res = string.Empty;
            int index = 0;
            int len = 0;
            OpenDcmFile a = new OpenDcmFile();
            a.Load(dcmFile);
            ByteBuffer b = a.ds.GetByteBuffer(FileDicom.FILETAG);
            byte[] data = b.ToArray();
            len = data.Length;
            string fileName = a.ds.GetStringByEncoding(FileDicom.FILENAMETAG, Encoding.Default);
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + "_" + fileName;
            string type = a.ds.GetStringByEncoding(FileDicom.FILETYPETAG, Encoding.Default);
            if (!string.IsNullOrEmpty(fileType)) type = fileType;
            res = Path.Combine(outDirectory, fileName + "." + type);
            try
            {
                FileStream fs1 = new FileStream(res, FileMode.OpenOrCreate);
                if (data.Length > 4)
                {
                    byte[] srLenByte = new byte[4];
                    srLenByte[0] = data[0];
                    srLenByte[1] = data[1];
                    srLenByte[2] = data[2];
                    srLenByte[3] = data[3];
                    int srLen = BitConverter.ToInt32(srLenByte, 0);
                    if ((srLen + 5 == data.Length) || (srLen + 4 == data.Length))
                    {
                        index = 4;
                        len -= 4;
                    }
                }
                fs1.Write(data, index, len);
                fs1.Close();
            }
            catch { }
            return res;
        }

        public string ParseFile(string dcmFile, string outDirectory, out string req_procid, string type = "")
        {
            string res = string.Empty;
            int index = 0;
            int len = 0;
            OpenDcmFile a = new OpenDcmFile();
            a.Load(dcmFile);
            req_procid = a.ds.GetStringByEncoding(Tags.AccessionNumber, Encoding.Default);
            ByteBuffer b = a.ds.GetByteBuffer(FileDicom.FILETAG);
            byte[] data = b.ToArray();
            len = data.Length;
            string fileName = a.ds.GetStringByEncoding(FileDicom.FILENAMETAG, Encoding.Default);
            string fileType = a.ds.GetStringByEncoding(FileDicom.FILETYPETAG, Encoding.Default);
            if (!string.IsNullOrEmpty(type)) fileType = type;
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssffffff") + "_" + fileName;
            res = Path.Combine(outDirectory, fileName + "." + fileType);
            try
            {
                FileStream fs1 = new FileStream(res, FileMode.OpenOrCreate);
                if (data.Length > 4)
                {
                    byte[] srLenByte = new byte[4];
                    srLenByte[0] = data[0];
                    srLenByte[1] = data[1];
                    srLenByte[2] = data[2];
                    srLenByte[3] = data[3];
                    int srLen = BitConverter.ToInt32(srLenByte, 0);
                    if ((srLen + 5 == data.Length) || (srLen + 4 == data.Length))
                    {
                        index = 4;
                        len -= 4;
                    }
                }
                fs1.Write(data, index, len);
                fs1.Close();
            }
            catch { }
            return res;
        }

        private DcmDataset GenerateStanderDcmDataset(DicomFileInfo info)
        {
            DcmDataset ds = new DcmDataset();

            FileMetaInfo fmi = new FileMetaInfo();
            fmi.PutOB(Tags.FileMetaInformationVersion, new byte[] { 0, 1 });
            fmi.PutUI(Tags.MediaStorageSOPClassUID, UIDs.BasicTextSR);
            fmi.PutUI(Tags.MediaStorageSOPInstanceUID, "1.2.276.0.7230010.3.1.4.3580585151.2940.1297390871.2");
            fmi.PutUI(Tags.TransferSyntaxUID, UIDs.ExplicitVRLittleEndian);
            fmi.PutCS(Tags.ImplementationClassUID, "1.2.276.0.7230010.3.0.3.5.4");
            fmi.PutSH(Tags.ImplementationVersionName, "bluesky");
            ds.SetFileMetaInfo(fmi);

            ds.PutCS(Tags.PatientID, info.PatientID);
            ds.PutCS(Tags.PatientName, info.PatientName);
            ds.PutDA(Tags.PatientBirthDate, info.PatientBirthDate);
            ds.PutCS(Tags.PatientSex, info.PatientSex);


            ds.PutUI(Tags.StudyInstanceUID, info.StudyInsUID);
            ds.PutDA(Tags.StudyDate, info.StudyDate);
            ds.PutTM(Tags.StudyTime, info.StudyTime);
            ds.PutCS(Tags.AccessionNumber, info.AccessionNum);
            ds.PutCS(Tags.StudyID, info.StudyID);

            ds.PutAS(Tags.PatientAge, info.PatientAge);

            ds.PutCS(Tags.Modality, info.Modality);
            ds.PutUI(Tags.SeriesInstanceUID, info.SeriesUID);
            ds.PutIS(Tags.SeriesNumber, info.SeriesNum);

            ds.PutCS(Tags.SpecificCharacterSet, "ISO_IR 100");
            ds.PutUI(Tags.SOPClassUID, "1.2.840.10008.5.1.4.1.1.88.59");
            ds.PutUI(Tags.SOPInstanceUID, info.SopUID);
            ds.PutCS(Tags.InstanceNumber, info.InsNum);

            ds.PutCS((uint)0x00810001, info.FileType);
            ds.PutCS((uint)0x00810002, info.FileName);
            FileStream stream = new FileStream(info.FilePath, FileMode.Open);
            byte[] bb = new byte[stream.Length];
            stream.Read(bb, 0, (int)stream.Length);
            ds.PutOB(FILETAG, bb);
            stream.Close();

            return ds;
        }
    }

DicomFileInfo辅助类:

 public class DicomFileInfo
    {
        private string accessionNum = null;
        private string modality = null;
        private string filePath = null;
        private string insNum = null;
        private string patientAge = null;
        private string patientBirthDate = null;
        private string patientID = null;
        private string seriesNum = null;
        private string patientName = null;
        private string patientSex = null;
        private string seriesUID = null;
        private string sopUID = null;
        private string studyDate = null;
        private string studyID = null;
        private string studyInsUID = null;
        private string studyTime = null;
        private string fileName = null;
        private string fileType = null;

        public string Modality
        {
            get { return modality; }
            set { modality = value; }
        }

        public string FileType
        {
            get { return fileType; }
            set { fileType = value; }
        }

        public string FileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        public string FilePath
        {
            get { return filePath; }
            set { filePath = value; }
        }

        public string AccessionNum
        {
            get
            {
                return this.accessionNum;
            }
            set
            {
                this.accessionNum = value;
            }
        }

        public string InsNum
        {
            get
            {
                return this.insNum;
            }
            set
            {
                this.insNum = value;
            }
        }

        public string PatientAge
        {
            get
            {
                return this.patientAge;
            }
            set
            {
                this.patientAge = value;
            }
        }

        public string PatientBirthDate
        {
            get
            {
                return this.patientBirthDate;
            }
            set
            {
                this.patientBirthDate = value;
            }
        }

        public string PatientID
        {
            get
            {
                return this.patientID;
            }
            set
            {
                this.patientID = value;
            }
        }

        public string PatientName
        {
            get
            {
                return this.patientName;
            }
            set
            {
                this.patientName = value;
            }
        }

        public string PatientSex
        {
            get
            {
                return this.patientSex;
            }
            set
            {
                this.patientSex = value;
            }
        }

        public string SeriesNum
        {
            get
            {
                return this.seriesNum;
            }
            set
            {
                this.seriesNum = value;
            }
        }

        public string SeriesUID
        {
            get
            {
                return this.seriesUID;
            }
            set
            {
                this.seriesUID = value;
            }
        }

        public string SopUID
        {
            get
            {
                return this.sopUID;
            }
            set
            {
                this.sopUID = value;
            }
        }

        public string StudyDate
        {
            get
            {
                return this.studyDate;
            }
            set
            {
                this.studyDate = value;
            }
        }

        public string StudyID
        {
            get
            {
                return this.studyID;
            }
            set
            {
                this.studyID = value;
            }
        }

        public string StudyInsUID
        {
            get
            {
                return this.studyInsUID;
            }
            set
            {
                this.studyInsUID = value;
            }
        }

        public string StudyTime
        {
            get
            {
                return this.studyTime;
            }
            set
            {
                this.studyTime = value;
            }
        }

    }


发表评论:

验证码