/// <summary> /// Serialize DVT Detail Data to Xml. /// </summary> /// <param name="streamWriter">Stream writer to serialize to.</param> /// <param name="level">Recursion level. 0 = Top.</param> /// <returns>bool - success/failure</returns> public bool DvtDetailToXml(StreamWriter streamWriter, int level) { FileHead.DvtDetailToXml(streamWriter, level); FileMetaInformation.DvtDetailToXml(streamWriter, level); DataSet.DvtDetailToXml(streamWriter, level); return(true); }
/// <summary> /// Write a dicom file object to a (persistent) Media Storage file. /// </summary> /// <param name="file">dicom file object to write</param> /// <param name="mediaFileName">file name to write to</param> /// <returns></returns> public static bool WriteDataSetToFile( DicomFile file, string mediaFileName) { if (file == null) throw new System.ArgumentNullException("file"); if (mediaFileName == null) throw new System.ArgumentNullException("mediaFileName"); FileMetaInformation fmi = file.FileMetaInformation; if (fmi == null) { fmi = new FileMetaInformation(); //Set the default transfer syntax(ELE) attribute in FMI fmi.AddAttribute("0x00020010",VR.UI,"1.2.840.10008.1.2.1"); file.FileMetaInformation = fmi; } return MDataSet.WriteFile(file, mediaFileName); }
/// <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; }