private bool SendDimseStream(byte pcid, DcmCommand command, Stream datastream) { try { _disableTimeout = true; DicomTransferSyntax ts = _assoc.GetAcceptedTransferSyntax(pcid); DcmDimseProgress progress = new DcmDimseProgress(); progress.EstimatedCommandLength = (int)command.CalculateWriteLength(DicomTransferSyntax.ImplicitVRLittleEndian, DicomWriteOptions.Default | DicomWriteOptions.CalculateGroupLengths); if (datastream != null) progress.EstimatedDatasetLength = (int)datastream.Length - (int)datastream.Position; PDataTFStream pdustream = new PDataTFStream(_network, pcid, (int)_assoc.MaximumPduLength); pdustream.OnPduSent += delegate() { progress.BytesTransfered = pdustream.BytesSent; OnSendDimseProgress(pcid, command, null, progress); }; lock (_socket) { OnSendDimseBegin(pcid, command, null, progress); DicomStreamWriter dsw = new DicomStreamWriter(pdustream); dsw.Write(command, DicomWriteOptions.Default | DicomWriteOptions.CalculateGroupLengths); dsw = null; if (datastream != null) { pdustream.IsCommand = false; pdustream.Write(datastream); } // last pdu is automatically flushed when streaming //pdustream.Flush(true); OnSendDimse(pcid, command, null, progress); } return true; } catch (Exception e) { #if DEBUG Log.Error("{0} -> Error sending DIMSE: {1}", LogID, e.ToString()); #else Log.Error("{0} -> Error sending DIMSE: {1}", LogID, e.Message); #endif OnNetworkError(e); return false; } finally { _disableTimeout = false; } }
private bool SendDimse(byte pcid, DcmCommand command, DcmDataset dataset) { try { _disableTimeout = true; DicomTransferSyntax ts = _assoc.GetAcceptedTransferSyntax(pcid); if (dataset != null && ts != dataset.InternalTransferSyntax) { if (ts.IsEncapsulated || dataset.InternalTransferSyntax.IsEncapsulated) throw new DicomNetworkException("Unable to transcode encapsulated transfer syntax!"); dataset.ChangeTransferSyntax(ts, null); } DcmDimseProgress progress = new DcmDimseProgress(); progress.EstimatedCommandLength = (int)command.CalculateWriteLength(DicomTransferSyntax.ImplicitVRLittleEndian, DicomWriteOptions.Default | DicomWriteOptions.CalculateGroupLengths); if (dataset != null) progress.EstimatedDatasetLength = (int)dataset.CalculateWriteLength(ts, DicomWriteOptions.Default); PDataTFStream pdustream = new PDataTFStream(_network, pcid, (int)_assoc.MaximumPduLength); pdustream.OnPduSent += delegate() { progress.BytesTransfered = pdustream.BytesSent; OnSendDimseProgress(pcid, command, dataset, progress); }; lock (_socket) { OnSendDimseBegin(pcid, command, dataset, progress); DicomStreamWriter dsw = new DicomStreamWriter(pdustream); dsw.Write(command, DicomWriteOptions.Default | DicomWriteOptions.CalculateGroupLengths); if (dataset != null) { pdustream.IsCommand = false; dsw.Write(dataset, DicomWriteOptions.Default); } // flush last pdu pdustream.Flush(true); OnSendDimse(pcid, command, dataset, progress); } return true; } catch (Exception e) { #if DEBUG Log.Error("{0} -> Error sending DIMSE: {1}", LogID, e.ToString()); #else Log.Error("{0} -> Error sending DIMSE: {1}", LogID, e.Message); #endif OnNetworkError(e); return false; } finally { _disableTimeout = false; } }
/// <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(); } }
/// <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(); } } }
/// <summary> /// Saves a DICOM file to a stream. <br/><i>Note that the caller is expected to dispose of the stream.</i> /// </summary> /// <param name="stream">Stream to place the DICOM file in</param> /// <param name="options">DICOM write options</param> public void Save(Stream stream, DicomWriteOptions options) { stream.Seek(128, SeekOrigin.Begin); stream.WriteByte((byte)'D'); stream.WriteByte((byte)'I'); stream.WriteByte((byte)'C'); stream.WriteByte((byte)'M'); DicomStreamWriter dsw = new DicomStreamWriter(stream); dsw.Write(_metainfo, options | DicomWriteOptions.CalculateGroupLengths); if (_dataset != null) dsw.Write(_dataset, options); }
/// <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; }