LaurelBridge.DCFExamples.DicomStreamIO Namespace

DICOM Connectivity Framework V3.4
The DicomStreamIO example demonstrates how to use the DCF to read and write DICOM datasets to Chapter 10 formatted binary streams.

Public classOptions
Command line options class for parsing user options for DicomStreamIO.
Public classProgram
This example shows several examples of using DCF to read and write DICOM Chapter 10 datasets to and from memory or file streams.

Supported OS Platforms:

  • Windows - .Net Framework 4.7.2 64-bit and 32-bit
  • Windows - .Net Core 2.1 64-bit and 32-bit
  • Linux - .Net Core 2.1 64-bit


DicomStreamIO Sample Code
public class Program
    /// <summary>
    /// Main entry point for DicomStreamIO.
    /// </summary>
    /// <param name="args">Program arguments.</param>
    public static void Main(string[] args)
            Options options;
            if (!Options.TryParse(args, out options))
                throw new ArgumentException("bad command line parameters");

            // get args
            if (!File.Exists(options.InputPath))
                throw new FileNotFoundException("DicomStreamIO needs an existing DICOM input file", options.InputPath);
            string outputPath = MakeStreamOutputPath(options.InputPath);

            // set PreserveGroupLengths so they are not stripped on output
            DicomSessionSettings ss = new DicomSessionSettings() { PreserveGroupLengths = true };
            // read the Chapter10 file into a memory stream
            Stream msInput = new MemoryStream(File.ReadAllBytes(options.InputPath), false);

            // read a Chapter10 dataset from a memory stream
            using (DicomFileInput dfi = new DicomFileInput(new DicomStreamReader(msInput, ss), "", ss))
                DicomDataSet dds = dfi.ReadDataSet();
                dds.ExpandStreamingModeData(true); // so we can compare later, otherwise pixel data can only be used once

                // write a Chapter10 dataset to a memory stream
                MemoryStream msOutput = new MemoryStream();

                // leaveOpen is set here to keep the backing MemoryStream open after disposing the DicomStreamWriter
                using (DicomFileOutput dfo = new DicomFileOutput(new DicomStreamWriter(msOutput, true), dfi.ActualTsUid, null, null, ss))
                    // Turning this off preserves any existing group 2 fields
                    dfo.CreateChapter10Format = false;

                // write the Chapter10 formatted memory stream to an output file
                using (FileStream fs = new FileStream(outputPath, FileMode.Create))
                    msOutput.Seek(0, SeekOrigin.Begin);

                // Read the Chapter10 file back as a dataset
                using (DicomFileInput dfi2 = new DicomFileInput(outputPath, ss))
                    DicomDataSet dds2 = dfi2.ReadDataSet();

                    // report differences
                    IList<IDicomDifference> diffs = new List<IDicomDifference>(new DicomComparer().GetDifferences(dds, dds2));
                    Console.WriteLine("There were {0} differences", diffs.Count);
                    foreach (IDicomDifference diff in diffs)
        catch (Exception e)
            Console.WriteLine("Error during execution: {0}", e);
            Environment.ExitCode = 1;
            if (System.Diagnostics.Debugger.IsAttached)
                Console.Write("Press any key to continue . . . ");

    /// <summary>
    /// Create an output path for the given inputPath.
    /// </summary>
    /// <param name="inputPath">The input path.</param>
    /// <returns>An output path that is near the input path.</returns>
    private static string MakeStreamOutputPath(string inputPath)
        StringBuilder sb = new StringBuilder(inputPath);
        if (inputPath.EndsWith(".dcm", StringComparison.CurrentCultureIgnoreCase))
            sb.Length -= 4;
        return sb.ToString();