LaurelBridge.DCF.Examples.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 classProgram
This example shows several examples of using DCF to read and write DICOM Chapter 10 datasets to and from memory or file streams.

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)
            // get args
            string inputPath = args.Length == 0 ? "mr-knee.dcm" : args[0];
            if (!File.Exists(inputPath))
                throw new FileNotFoundException("DicomStreamIO needs an existing DICOM input file", inputPath);
            string outputPath = MakeStreamOutputPath(inputPath);

            // read the Chapter10 file into a memory stream
            DicomSessionSettings ss = new DicomSessionSettings();
            MemoryStream msInput = new MemoryStream(File.ReadAllBytes(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();
                using (DicomFileOutput dfo = new DicomFileOutput(new DicomStreamWriter(msOutput), 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
                byte[] bytes = msOutput.ToArray();
                File.WriteAllBytes(outputPath, bytes);

                // 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();