LAUREL BRIDGE

LaurelBridge.DCFExamples.DicomToImage Namespace

DICOM Connectivity Framework V3.4
The DicomToImage example demonstrates how to use the DCF to read a DICOM image file into a DicomImage object, get its Windows Bitmap, and save that Bitmap in one of the common image formats (BMP, GIF, JPEG, PNG, TIF).
Classes

  ClassDescription
Public classOptions
Command line options class for parsing user options for SplitMultiframeImage
Public classProgram
An example program to demonstrate how to convert a DICOM dataset containing pixel data to a common image format.
Remarks

Supported OS Platforms:

  • Windows - .Net Framework 4.7.2 64-bit and 32-bit

Examples

DicomToImage Sample Code
public class Program
{
    private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();

    /// <summary>
    /// Main entry point for DicomToImage.
    /// </summary>
    /// <param name="args">command line arguments</param>
    [STAThread]
    public static void Main(string[] args)
    {
        try
        {
            Options options;
            if (!Options.TryParse(args, out options))
            {
                throw new ArgumentException("bad command line parameters");
            }

            string inputPath = options.InputPath;
            if (!File.Exists(inputPath))
            {
                throw new FileNotFoundException("input file path not found", inputPath);
            }

            string outputPath = options.OutputPath;
            string outputDirectory = Path.GetDirectoryName(Path.GetFullPath(outputPath));
            if (!Directory.Exists(outputDirectory))
            {
                throw new DirectoryNotFoundException("output path directory does not exist");
            }

            // Convert DICOM to selected image
            ConvertDicomToImage(inputPath, outputPath);
            Console.WriteLine("Saved {0} to {1}", inputPath, outputPath);
        }
        catch (Exception e)
        {
            Logger.Error(e, "DicomToImage: failed: ");
            Environment.ExitCode = 1;
        }
        finally
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.Write(@"Press any key to continue . . . ");
                Console.ReadKey();
            }
        }
    }

    /// <summary>
    /// Reads a DICOM image and saves it in a common image format.
    /// Only the first frame of a multi-frame DICOM image will be saved.
    /// The format of the saved image is determined by the extension of
    /// imageFilename.
    /// </summary>
    /// <param name="dicomFilename">Path to the selected DICOM file to be converted</param>
    /// <param name="imageFilename">Path for saving the new image file</param>
    private static void ConvertDicomToImage(string dicomFilename, string imageFilename)
    {
        // Read in the DICOM image
        DicomImage dicomImage = DicomImage.CreateFromFile(dicomFilename);

        // Save it as an image. The parameterless version of GetFrame returns the
        // first frame (index 0). For multi-frame images, passing a frame index to
        // GetFrame returns the frame at that index.
        Bitmap bitmap = dicomImage.GetFrame();
        ImageFormat imageFormat = GetImageFormatFromFilename(imageFilename);
        bitmap.Save(imageFilename, imageFormat);
    }

    /// <summary>
    /// Determines a Windows common ImageFormat from the extension of ImageFilename.
    /// </summary>
    /// <param name="imageFilename">Path for saving the new image file</param>
    /// <returns>ImageFormat corresponding to the extension of imageFilename</returns>
    private static ImageFormat GetImageFormatFromFilename(string imageFilename)
    {
        string extension = Path.GetExtension(imageFilename);
        if (string.IsNullOrEmpty(extension))
        {
            throw new ArgumentException(@"Cannot determine file extension for filename: {0}", imageFilename);
        }

        // Decode the image format from the image filename extension
        switch (extension.ToLower(CultureInfo.InvariantCulture))
        {
            case ".bmp":
                return ImageFormat.Bmp;
            case ".gif":
                return ImageFormat.Gif;
            case ".jpg":
            case ".jpeg":
                return ImageFormat.Jpeg;
            case ".png":
                return ImageFormat.Png;
            case ".tif":
            case ".tiff":
                return ImageFormat.Tiff;
            default:
                throw new ArgumentException("Cannot determine ImageFormat for filename: {0}", imageFilename);
        }
    }
}