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
Class | Description | |
---|---|---|
Program |
An example program to demonstrate how to convert a DICOM dataset containing pixel data to a common image format.
| |
ViewShowNothingDialog |
Minimalist three-button dialog that enables a user to select from
three choices: View Image, Show in Folder, or Nothing.
|
Examples
DicomToImage Sample Code
public class Program { private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); /// <summary> /// The main entry point for DicomToImage. /// </summary> [STAThread] public static void Main() { int status = 1; try { // Select a DICOM File string dicomFilename = SelectDicomFile(); if (string.IsNullOrEmpty(dicomFilename)) { Console.WriteLine(@"No DICOM file selected. Exiting."); return; } // Select an image path to save string imageFilename = SelectImageSavePath(dicomFilename); if (string.IsNullOrEmpty(imageFilename)) { Console.WriteLine(@"No image to save was selected. Exiting."); return; } // Convert DICOM to selected image ConvertDicomToImage(dicomFilename, imageFilename); Console.WriteLine(@"Saved {0} to {1}", dicomFilename, imageFilename); // View the image, show its folder, or simply exit ViewShowOrExit(imageFilename); status = 0; } catch (Exception e) { status = 2; Logger.Error(e, "DicomToImage: failed: "); } finally { if (System.Diagnostics.Debugger.IsAttached) { Console.Write(@"Press any key to continue . . . "); Console.ReadKey(); } Environment.Exit(status); } } /// <summary> /// Selects a DICOM file using the Windows OpenFileDialog class. /// </summary> /// <returns>Path to the selected DICOM file, or null if no file was selected</returns> private static string SelectDicomFile() { using (OpenFileDialog ofd = new OpenFileDialog()) { ofd.Title = @"Select a DICOM File to Convert"; ofd.Filter = @"DICOM Image Files|*.dcm|All Files|*.*"; ofd.RestoreDirectory = true; if (ofd.ShowDialog() == DialogResult.OK) { return ofd.FileName; } } return null; } /// <summary> /// Selects an image format and filename for saving the output image. /// It uses the original DICOM filename as a starting point for the /// new image filename by removing its extension and appending the /// extension of the selected image type. /// </summary> /// <param name="dicomFilename">Path to the selected DICOM file to be converted</param> /// <returns>Path for saving the new image file, or null if no file was selected</returns> private static string SelectImageSavePath(string dicomFilename) { using (SaveFileDialog sfd = new SaveFileDialog()) { sfd.Title = @"Save DICOM Image As"; sfd.FileName = Path.GetFileNameWithoutExtension(dicomFilename); sfd.Filter = @"BMP|*.bmp|GIF|*.gif|JPEG|*.jpg;*.jpeg|PNG|*.png|TIFF|*.tif;*.tiff"; sfd.FilterIndex = 3; sfd.DefaultExt = ".jpg"; sfd.AddExtension = true; if (sfd.ShowDialog() == DialogResult.OK) { return sfd.FileName; } } return null; } /// <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()) { 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); } } /// <summary> /// Convenience wrapper around the ViewShowNothing dialog that provides /// a three-way choice about whether the user wants to try to view /// the image in imageFilename, to view its enclosing folder, or /// to simply do nothing and return. /// </summary> /// <param name="imageFilename">Path of the image file</param> private static void ViewShowOrExit(string imageFilename) { DialogResult result = (new ViewShowNothingDialog()).ShowDialog(); if (result == DialogResult.Yes) { // "Running" an image file makes the system try to // open it using its default application, if any System.Diagnostics.Process.Start(imageFilename); } else if (result == DialogResult.OK) { // Selecting a file in explorer causes its directory // to be opened and the file to be selected System.Diagnostics.Process.Start("explorer.exe", "/select," + imageFilename); } } }