private void OnClickSaveWithTransferSyntax(object sender, EventArgs e) { if (_selected == -1) { return; } TransferSyntaxForm tsForm = new TransferSyntaxForm(); if (tsForm.ShowDialog(this) == DialogResult.OK) { SaveFileDialog sfd = new SaveFileDialog(); sfd.RestoreDirectory = true; if (sfd.ShowDialog(this) == DialogResult.OK) { DicomFileFormat ff = new DicomFileFormat(); ff.Load(_files[_selected], DicomReadOptions.Default); if (tsForm.SelectedTransferSyntax != null) { ff.ChangeTransferSytnax(tsForm.SelectedTransferSyntax, null); } ff.Save(sfd.FileName, DicomWriteOptions.Default); } } }
/// <summary> /// Loads the DICOM file and changes the transfer syntax if needed. (Internal) /// </summary> /// <param name="client">C-Store Client</param> public void Load(CStoreClient client) { if (_loaded) { return; } try { DicomTransferSyntax tx = null; foreach (DcmPresContext pc in client.Associate.GetPresentationContexts()) { if (pc.Result == DcmPresContextResult.Accept && pc.AbstractSyntax == _sopClass) { tx = pc.AcceptedTransferSyntax; break; } } if (tx == null) { throw new DicomNetworkException("No accepted presentation contexts for abstract syntax: " + _sopClass.Description); } // Possible to stream from file? if (!client.DisableFileStreaming && tx == TransferSyntax) { using (FileStream fs = DicomFileFormat.GetDatasetStream(_fileName)) { _datasetSize = Convert.ToUInt32(fs.Length - fs.Position); fs.Close(); } return; } DcmCodecParameters codecParams = null; if (tx == client.PreferredTransferSyntax) { codecParams = client.PreferredTransferSyntaxParams; } DicomFileFormat ff = new DicomFileFormat(); ff.Load(FileName, DicomReadOptions.DefaultWithoutDeferredLoading); if (_originalTransferSyntax != tx) { if (_originalTransferSyntax.IsEncapsulated) { // Dataset is compressed... decompress try { ff.ChangeTransferSytnax(DicomTransferSyntax.ExplicitVRLittleEndian, null); } catch { client.Log.Error("{0} -> Unable to change transfer syntax:\n\tclass: {1}\n\told: {2}\n\tnew: {3}\n\treason: {4}\n\tcodecs: {5} - {6}", client.LogID, SOPClassUID.Description, _originalTransferSyntax, DicomTransferSyntax.ExplicitVRLittleEndian, #if DEBUG HasError ? "Unknown" : Error.ToString(), #else HasError ? "Unknown" : Error.Message, #endif DicomCodec.HasCodec(_originalTransferSyntax), DicomCodec.HasCodec(DicomTransferSyntax.ExplicitVRLittleEndian)); throw; } } if (tx.IsEncapsulated) { // Dataset needs to be compressed try { ff.ChangeTransferSytnax(tx, codecParams); } catch { client.Log.Error("{0} -> Unable to change transfer syntax:\n\tclass: {1}\n\told: {2}\n\tnew: {3}\n\treason: {4}\n\tcodecs: {5} - {6}", client.LogID, SOPClassUID.Description, ff.Dataset.InternalTransferSyntax, tx, #if DEBUG HasError ? "Unknown" : Error.ToString(), #else HasError ? "Unknown" : Error.Message, #endif DicomCodec.HasCodec(ff.Dataset.InternalTransferSyntax), DicomCodec.HasCodec(tx)); throw; } } } _dataset = ff.Dataset; _datasetSize = _dataset.CalculateWriteLength(tx, DicomWriteOptions.Default); _transferSyntax = tx; } catch (Exception e) { _dataset = null; _transferSyntax = _originalTransferSyntax; _status = DcmStatus.ProcessingFailure; _exception = e; } finally { _loaded = true; } }