예제 #1
0
        /// <summary>
        /// Constructor.
        ///
        /// Use this constructor to create a new presentation context instance that is to be used in an A-ASSOCIATE-RQ.
        /// </summary>
        /// <param name="abstractSyntax">The abstract syntax.</param>
        /// <param name="transferSyntaxes">The transfer syntaxes (must contain at least one transfer syntax).</param>
        /// <exception cref="System.ArgumentException">No transfer syntaxes have been supplied.</exception>
        public PresentationContext(String abstractSyntax, params String[] transferSyntaxes)
        {
            //
            // Check parameter(s).
            //

            if (transferSyntaxes.Length < 1)
            {
                throw new System.ArgumentException("At least of transfer syntax must be supplied in the PresentationContext constructor.");
            }


            //
            // Create the encapsulated DvtkData instance.
            //

            DvtkData.Dul.AbstractSyntax dvtkDataAbstractSyntax = new DvtkData.Dul.AbstractSyntax(abstractSyntax);

            DvtkData.Dul.TransferSyntax[] dvtkDataTransferSyntaxes = new DvtkData.Dul.TransferSyntax[transferSyntaxes.Length];
            for (int index = 0; index < transferSyntaxes.Length; index++)
            {
                dvtkDataTransferSyntaxes[index] = new DvtkData.Dul.TransferSyntax(transferSyntaxes[index]);
            }

            this.dvtkDataRequestedPresentationContext = new DvtkData.Dul.RequestedPresentationContext(dvtkDataAbstractSyntax, dvtkDataTransferSyntaxes);
        }
예제 #2
0
파일: DICOMEditor.cs 프로젝트: ewcasas/DVTK
        void SaveFileAsDefinedTS(string transferSyntax)
        {
            if (DCMFile == "")
            {
                richTextBoxLog.AppendText("There is no Media file to save, please select Media file.\n");
                return;
            }

            try
            {
                SaveFileDialog saveAsDlg = new SaveFileDialog();
                saveAsDlg.Filter = "DCM files (*.dcm) |*.dcm | All files (*.*)|*.*";
                FileInfo dcmFileInfo = new FileInfo(DCMFile);
                saveAsDlg.InitialDirectory = dcmFileInfo.DirectoryName;
                saveAsDlg.FileName = dcmFileInfo.Name;
                saveAsDlg.Title = "Save DICOM file...";
                if (saveAsDlg.ShowDialog() == DialogResult.OK)
                {
                    richTextBoxLog.AppendText("Saving the modified Media file.\n");

                    _NewDCMFileName = saveAsDlg.FileName.Trim();

                    if (DCMFile != _NewDCMFileName)
                        _IsSavedInNewDCMFile = true;

                    DvtkData.Media.DicomFile dicomMediaFile = new DvtkData.Media.DicomFile();

                    // set up the file head
                    DvtkData.Media.FileHead fileHead = new DvtkData.Media.FileHead();

                    // add the Transfer Syntax UID
                    DvtkData.Dul.TransferSyntax tSyntax = new DvtkData.Dul.TransferSyntax(transferSyntax);
                    fileHead.TransferSyntax = tSyntax;

                    // set up the file meta information
                    DvtkData.Media.FileMetaInformation fileMetaInformation = new DvtkData.Media.FileMetaInformation();

                    // add the FMI version
                    fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.FILE_META_INFORMATION_VERSION.GroupNumber,
                        DvtkData.Dimse.Tag.FILE_META_INFORMATION_VERSION.ElementNumber, VR.OB, 1, 2);

                    // add the SOP Class UID
                    fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_CLASS_UID.GroupNumber,
                        DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_CLASS_UID.ElementNumber, VR.UI, _FileMetaInfo.MediaStorageSOPClassUID);

                    // add the SOP Instance UID
                    fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.GroupNumber,
                        DvtkData.Dimse.Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.ElementNumber, VR.UI, _FileMetaInfo.MediaStorageSOPInstanceUID);

                    // add the Transfer Syntax UID
                    fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID.GroupNumber,
                        DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID.ElementNumber, VR.UI, transferSyntax);

                    // add the Implemenation Class UID
                    string implClassUID = "";
				    if(_FileMetaInfo.Exists("0x00020012"))
				    {
					    // Get the Transfer syntax
					    HLI.Attribute implClassUIDAttr = _FileMetaInfo["0x00020012"];
					    implClassUID = implClassUIDAttr.Values[0];
				    }

                    fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.IMPLEMENTATION_CLASS_UID.GroupNumber,
                        DvtkData.Dimse.Tag.IMPLEMENTATION_CLASS_UID.ElementNumber, VR.UI, implClassUID);

                    // add the Implementation Version Name
                    string implClassVersion = "";
				    if(_FileMetaInfo.Exists("0x00020013"))
				    {
					    // Get the Transfer syntax
					    HLI.Attribute implClassVersionAttr = _FileMetaInfo["0x00020013"];
					    implClassVersion = implClassVersionAttr.Values[0];
				    }

                    fileMetaInformation.AddAttribute(DvtkData.Dimse.Tag.IMPLEMENTATION_VERSION_NAME.GroupNumber,
                        DvtkData.Dimse.Tag.IMPLEMENTATION_VERSION_NAME.ElementNumber, VR.SH, implClassVersion);

                    // set up the dicomMediaFile contents
                    dicomMediaFile.FileHead = fileHead;
                    dicomMediaFile.FileMetaInformation = fileMetaInformation;
                    dicomMediaFile.DataSet = _DCMdataset.DvtkDataDataSet;

                    // write the dicomMediaFile to file
                    Dvtk.DvtkDataHelper.WriteDataSetToFile(dicomMediaFile, _NewDCMFileName);

                    string theLogText = string.Format("Dataset {0} saved successfully with transfer syntax: {1}.\n\n", _NewDCMFileName, transferSyntax);

                    richTextBoxLog.AppendText(theLogText);
                    richTextBoxLog.ScrollToCaret();
                    richTextBoxLog.Focus();

                    //Cleanup the FMI & Dataset
                    IsDCMFileModified = false;
                    _IsAttributeGroupLengthDefined = false;
                    _DCMdataset = null;
                    _FileMetaInfo = null;

                    if (_IsSavedInNewDCMFile)
                    {
                        FileInfo newDcmFileInfo = new FileInfo(_NewDCMFileName);

                        _IsNewDCMFileLoaded = true;
                        this.dirListBox.Path = newDcmFileInfo.DirectoryName;
                        this.fileListBox.Path = this.dirListBox.Path;
                        this.fileListBox.SelectedItem = newDcmFileInfo.Name;

                        this.dirListBox.Refresh();
                        this.fileListBox.Refresh();

                        //Load the new Media file
                        LoadDCMFile(_NewDCMFileName);
                    }
                    else
                    {
                        _IsNewDCMFileLoaded = false;
                        this.dirListBox.Path = dcmFileInfo.DirectoryName;
                        this.fileListBox.Path = this.dirListBox.Path;
                        this.fileListBox.SelectedItem = dcmFileInfo.Name;

                        //Load the new Media file
                        LoadDCMFile(DCMFile);
                    }

                    UpdateTitleBarText();
                }
            }
            catch (Exception exception)
            {
                string theErrorText;

                theErrorText = string.Format("Media file {0} could not be saved:\n{1}\n\n", _NewDCMFileName, exception.Message);

                richTextBoxLog.AppendText(theErrorText);
                richTextBoxLog.ScrollToCaret();
                richTextBoxLog.Focus();

                IsDCMFileModified = false;
                UpdateTitleBarText();
            }            
        }
예제 #3
0
파일: Association.cs 프로젝트: ewcasas/DVTK
        /// <summary>
        /// Save selected dataset as DCM FIle
        /// </summary>
        /// <param name="data"></param>
        /// <param name="filename"></param>
        public bool DumpAsDCMFile(byte[] data, string tranferSyntax, string filename)
        {
            bool ok = false;

            //Set Dataset
            string tempFile = Application.StartupPath + @"\Dcm\temp.dcm";
            using(FileStream f=new FileStream(tempFile,FileMode.Create))
            {
                f.Write(data,0,data.Length);
                f.Close();
            }

            //Read Dataset from the file
            DataSet dataset = Dvtk.DvtkDataHelper.ReadDataSetFromFile(tempFile,true);

            //Delete the temp file
            FileInfo tempfile = new FileInfo(tempFile);
            tempfile.Delete();

            if(dataset == null)
            {
                return false;
            }

            DicomFile dicomMediaFile = new DicomFile();

            // set up the file head
            FileHead fileHead = new FileHead();

            // add the Transfer Syntax UID
            DvtkData.Dul.TransferSyntax transferSyntax = new DvtkData.Dul.TransferSyntax(DvtkData.Dul.TransferSyntax.Explicit_VR_Little_Endian.UID);
            fileHead.TransferSyntax = transferSyntax;

            // set up the file meta information
            FileMetaInformation fileMetaInformation = new FileMetaInformation();

            // add the FMI version
            fileMetaInformation.AddAttribute(Tag.FILE_META_INFORMATION_VERSION.GroupNumber,
                Tag.FILE_META_INFORMATION_VERSION.ElementNumber, VR.OB, 1, 2);

            // add the SOP Class UID
            System.String sopClassUid = "";
            DvtkData.Dimse.Attribute attribute = dataset.GetAttribute(Tag.SOP_CLASS_UID);
            if (attribute != null)
            {
                UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue;
                if (uniqueIdentifier.Values.Count > 0)
                {
                    sopClassUid = uniqueIdentifier.Values[0];
                }
            }
            fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_CLASS_UID.GroupNumber,
                Tag.MEDIA_STORAGE_SOP_CLASS_UID.ElementNumber, VR.UI, sopClassUid);

            // add the SOP Instance UID
            System.String sopInstanceUid = "";
            attribute = dataset.GetAttribute(Tag.SOP_INSTANCE_UID);
            if (attribute != null)
            {
                UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue;
                if (uniqueIdentifier.Values.Count > 0)
                {
                    sopInstanceUid = uniqueIdentifier.Values[0];
                }
            }
            fileMetaInformation.AddAttribute(Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.GroupNumber,
                Tag.MEDIA_STORAGE_SOP_INSTANCE_UID.ElementNumber, VR.UI, sopInstanceUid);

            // add the Transfer Syntax UID
            fileMetaInformation.AddAttribute(Tag.TRANSFER_SYNTAX_UID.GroupNumber,
                Tag.TRANSFER_SYNTAX_UID.ElementNumber, VR.UI, tranferSyntax);

            // add the Implemenation Class UID
            fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_CLASS_UID.GroupNumber,
                Tag.IMPLEMENTATION_CLASS_UID.ElementNumber, VR.UI, "1.2.826.0.1.3680043.2.1545.1");

            // add the Implementation Version Name
            fileMetaInformation.AddAttribute(Tag.IMPLEMENTATION_VERSION_NAME.GroupNumber,
                Tag.IMPLEMENTATION_VERSION_NAME.ElementNumber, VR.SH, "DNA");

            // set up the dicomMediaFile contents
            dicomMediaFile.FileHead = fileHead;
            dicomMediaFile.FileMetaInformation = fileMetaInformation;
            dicomMediaFile.DataSet = dataset;

            // write the dicomMediaFile to file
            if(Dvtk.DvtkDataHelper.WriteDataSetToFile(dicomMediaFile, filename))
            {
                string message = string.Format("DataSet logged to file {0} at {1}\r\n",filename,System.DateTime.Now);
                Out(message,true);
                ok = true;
            }

            //Clear all temporary pix files
            FileInfo file = new FileInfo(filename);
            ArrayList theFilesToRemove = new ArrayList();
            DirectoryInfo theDirectoryInfo = new DirectoryInfo(file.DirectoryName);
            FileInfo[] thePixFilesInfo;

            if (theDirectoryInfo.Exists)
            {
                thePixFilesInfo = theDirectoryInfo.GetFiles("*.pix");

                foreach (FileInfo theFileInfo in thePixFilesInfo)
                {
                    string thePixFileName = theFileInfo.Name;

                    theFilesToRemove.Add(thePixFileName);
                }
            }

            //Delete all pix files
            foreach(string theFileName in theFilesToRemove)
            {
                string theFullFileName = System.IO.Path.Combine(theDirectoryInfo.FullName, theFileName);

                if (File.Exists(theFullFileName))
                {
                    try
                    {
                        File.Delete(theFullFileName);
                    }
                    catch(Exception exception)
                    {
                        string theWarningText = string.Format("Could not be delete the {0} temporary file.\n due to exception: {1}\n\n", theFullFileName, exception.Message);

                        Out(theWarningText,true);
                    }
                }
            }

            return ok;
        }