Esempio n. 1
0
        /// <summary>
        /// Save the file as a DICOM Part 10 format file.
        /// </summary>
        /// <param name="options">The options to use when saving the file.</param>
        /// <param name="iStream">The <see cref="Stream"/> to Save the DICOM file to.</param>
        /// <returns></returns>
        public bool Save(Stream iStream, DicomWriteOptions options)
        {
            if (iStream == null)
            {
                throw new ArgumentNullException("iStream");
            }

            // Original code has seek() here, but there may be use cases where
            // a user wants to add the file into a stream (that may contain other data)
            // and the seek would cause the method to not support that.
            byte[] prefix = new byte[128];
            iStream.Write(prefix, 0, 128);

            iStream.WriteByte((byte)'D');
            iStream.WriteByte((byte)'I');
            iStream.WriteByte((byte)'C');
            iStream.WriteByte((byte)'M');

            DicomStreamWriter dsw = new DicomStreamWriter(iStream);

            dsw.Write(TransferSyntax.ExplicitVrLittleEndian,
                      MetaInfo, options | DicomWriteOptions.CalculateGroupLengths);

            MetaInfoFileLength = iStream.Position;

            dsw.Write(TransferSyntax, DataSet, options);

            iStream.Flush();

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Saves a DICOM file
        /// </summary>
        /// <param name="file">Filename</param>
        /// <param name="options">DICOM write options</param>
        public void Save(String file, DicomWriteOptions options)
        {
            string dir = Path.GetDirectoryName(file);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (FileStream fs = File.Create(file)) {
                fs.Seek(128, SeekOrigin.Begin);
                fs.WriteByte((byte)'D');
                fs.WriteByte((byte)'I');
                fs.WriteByte((byte)'C');
                fs.WriteByte((byte)'M');

                DicomStreamWriter dsw = new DicomStreamWriter(fs);
                dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths);
                if (_dataset != null)
                {
                    dsw.Write(_dataset, options);
                }

                fs.Close();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Saves a DICOM file in the isolated storage area
        /// </summary>
        /// <param name="file">Filename</param>
        /// <param name="options">DICOM write options</param>
        /// <param name="useIsoStore">Save in isolated storage</param>
        public void Save(string file, DicomWriteOptions options, bool useIsoStore = false)
        {
            if (useIsoStore)
            {
                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    string dir = Path.GetDirectoryName(file);
                    if (dir != null && !store.DirectoryExists(dir))
                    {
                        store.CreateDirectory(dir);
                    }

                    var fs = store.CreateFile(file);
                    fs.Seek(128, SeekOrigin.Begin);
                    fs.WriteByte((byte)'D');
                    fs.WriteByte((byte)'I');
                    fs.WriteByte((byte)'C');
                    fs.WriteByte((byte)'M');

                    DicomStreamWriter dsw = new DicomStreamWriter(fs);
                    dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths);
                    if (_dataset != null)
                    {
                        dsw.Write(_dataset, options);
                    }

                    fs.Close();
                }
            }
            else
            {
                // expand to full path
                file = Path.GetFullPath(file);

                string dir = Path.GetDirectoryName(file);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                using (FileStream fs = File.Create(file))
                {
                    fs.Seek(128, SeekOrigin.Begin);
                    fs.WriteByte((byte)'D');
                    fs.WriteByte((byte)'I');
                    fs.WriteByte((byte)'C');
                    fs.WriteByte((byte)'M');

                    DicomStreamWriter dsw = new DicomStreamWriter(fs);
                    dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths);
                    if (_dataset != null)
                    {
                        dsw.Write(_dataset, options);
                    }

                    fs.Close();
                }
            }
        }
Esempio n. 4
0
        public void OBAttributeOddLengthTests()
        {
            using(var ms = new MemoryStream())
            {
                var writer = new DicomStreamWriter(ms);

                var ds = new DicomAttributeCollection();
                var rawdata = new byte[] {0x0, 0x1, 0x2};

                ds[DicomTags.EncapsulatedDocument].Values = rawdata;
                writer.Write(TransferSyntax.ImplicitVrLittleEndian, ds, DicomWriteOptions.Default);

                ms.Position = 0;
                var output = ms.ToArray();
                var expectedOutput = new byte[]
                                         {
                                             0x42, 0x00, 0x11, 0x00,
                                             0x04, 0x00, 0x00, 0x00,
                                             0x0, 0x1, 0x2, 0x0 /* pad */
                                         };

                Assert.AreEqual(output, expectedOutput);
            }
            
        }
Esempio n. 5
0
        private static MemoryStream CreateNonPart10Stream(DicomAttributeCollection dataSet)
        {
            var memoryStream = new MemoryStream();
            var streamWriter = new DicomStreamWriter(memoryStream);

            streamWriter.TransferSyntax = TransferSyntax.ImplicitVrLittleEndian;
            streamWriter.Write(TransferSyntax.ImplicitVrLittleEndian, dataSet, DicomWriteOptions.None);
            memoryStream.Position = 0;
            return(memoryStream);
        }
Esempio n. 6
0
        /// <summary>
        /// Saves a DICOM file to a MemoryStream
        /// </summary>
        /// <param name="options">DICOM write options.</param>
        /// <returns></returns>
        public MemoryStream Save(DicomWriteOptions options)
        {
            MemoryStream ms = new MemoryStream();

            ms.Seek(128, SeekOrigin.Begin);
            ms.WriteByte((byte)'D');
            ms.WriteByte((byte)'I');
            ms.WriteByte((byte)'C');
            ms.WriteByte((byte)'M');

            DicomStreamWriter dsw = new DicomStreamWriter(ms);

            dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths);
            if (_dataset != null)
            {
                dsw.Write(_dataset, options);
            }

            return(ms);
        }
Esempio n. 7
0
        public void TextAttributeEvenLengthTests()
        {
            using (var ms = new MemoryStream())
            {
                var writer = new DicomStreamWriter(ms);

                var ds = new DicomAttributeCollection();

                ds[DicomTags.PatientId].Values = "PatientID0";
                writer.Write(TransferSyntax.ImplicitVrLittleEndian, ds, DicomWriteOptions.Default);

                ms.Position = 0;
                var output         = ms.ToArray();
                var expectedOutput = new byte[]
                {
                    0x10, 0x00, 0x20, 0x00,
                    0x0A, 0x00, 0x00, 0x00,
                    0x50, 0x61, 0x74, 0x69, 0x65, 0x6E, 0x74, 0x49, 0x44, 0x30                  /*PatientID0*/
                };

                Assert.AreEqual(output, expectedOutput);
            }
        }
Esempio n. 8
0
        public AuditQueryMessageParticipantObject(string sopClassUid, DicomAttributeCollection msg)
        {
            ParticipantObjectTypeCode         = ParticipantObjectTypeCodeEnum.SystemObject;
            ParticipantObjectTypeCodeRole     = ParticipantObjectTypeCodeRoleEnum.Report;
            ParticipantObjectIdTypeCodedValue = ParticipantObjectIDTypeCode.ClassUID;
            ParticipantObjectId     = sopClassUid;
            ParticipantObjectDetail = new ParticipantObjectDetail()
            {
                type  = "TransferSyntax",
                value = System.Text.Encoding.ASCII.GetBytes(TransferSyntax.ExplicitVrLittleEndianUid)
            };

            MemoryStream      ms     = new MemoryStream();
            DicomStreamWriter writer = new DicomStreamWriter(ms)
            {
                TransferSyntax = TransferSyntax.ExplicitVrLittleEndian
            };

            writer.Write(TransferSyntax.ExplicitVrLittleEndian, msg, DicomWriteOptions.Default);

            ParticipantObjectQuery = ms.ToArray();

            ParticipantObjectDetailString = CreateDetailsString(msg);
        }
Esempio n. 9
0
        public void OBAttributeOddLengthTests()
        {
            using (var ms = new MemoryStream())
            {
                var writer = new DicomStreamWriter(ms);

                var ds      = new DicomAttributeCollection();
                var rawdata = new byte[] { 0x0, 0x1, 0x2 };

                ds[DicomTags.EncapsulatedDocument].Values = rawdata;
                writer.Write(TransferSyntax.ImplicitVrLittleEndian, ds, DicomWriteOptions.Default);

                ms.Position = 0;
                var output         = ms.ToArray();
                var expectedOutput = new byte[]
                {
                    0x42, 0x00, 0x11, 0x00,
                    0x04, 0x00, 0x00, 0x00,
                    0x0, 0x1, 0x2, 0x0                          /* pad */
                };

                Assert.AreEqual(output, expectedOutput);
            }
        }
Esempio n. 10
0
        public void TextAttributeEvenLengthTests()
        {
            using (var ms = new MemoryStream())
            {
                var writer = new DicomStreamWriter(ms);

                var ds = new DicomAttributeCollection();

                ds[DicomTags.PatientId].Values = "PatientID0";
                writer.Write(TransferSyntax.ImplicitVrLittleEndian, ds, DicomWriteOptions.Default);

                ms.Position = 0;
                var output = ms.ToArray();
                var expectedOutput = new byte[]
                                         {
                                             0x10, 0x00, 0x20, 0x00,
                                             0x0A, 0x00, 0x00, 0x00,
                                             0x50, 0x61,0x74,0x69,0x65,0x6E,0x74,0x49,0x44,0x30 /*PatientID0*/
                                         };

                Assert.AreEqual(output, expectedOutput);
            }

        }