Page 1 of 1

Console: Convert a DICOM file to JPEG images.

Posted: Tue Feb 27, 2018 8:43 am
by Alex
This topic contains C# code sample that shows how to convert a DICOM file to JPEG images:

Code: Select all

using System;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("ConsoleApplication1.exe <path_to_DICOM_file>");
                return;
            }

            string dicomFilePath = args[0];

            if (!System.IO.File.Exists(dicomFilePath))
            {
                Console.WriteLine(string.Format("File \"{0}\" does not exist.", dicomFilePath));
                return;
            }

            // convert a DICOM file to a JPEG files
            ConvertDicomToJpeg(dicomFilePath);
        }

        /// <summary>
        /// Converts a DICOM file to a JPEG files.
        /// </summary>
        /// <param name="dicomFileName">Name of the input DICOM file.</param>
        public static void ConvertDicomToJpeg(string dicomFileName)
        {
            // create image collection
            using (Vintasoft.Imaging.ImageCollection imageCollection =
                new Vintasoft.Imaging.ImageCollection())
            {
                // add DICOM file to the collection
                imageCollection.Add(dicomFileName);

                // create JPEG encoder
                using (Vintasoft.Imaging.Codecs.Encoders.JpegEncoder jpegEncoder =
                    new Vintasoft.Imaging.Codecs.Encoders.JpegEncoder())
                {
                    // set the quality for output JPEG files
                    jpegEncoder.Settings.Quality = 70;

                    // for each image in image collection
                    for (int i = 0; i < imageCollection.Count; i++)
                    {
                        // save image as JPEG file
                        imageCollection[i].Save(string.Format("page{0}.jpg", i), jpegEncoder);
                        Console.Write(".");
                    }
                }

                Console.WriteLine(string.Format("{0} DICOM frames are saved.", imageCollection.Count));

                // clear and dispose images
                imageCollection.ClearAndDisposeItems();
            }
        }

    }
}

Source codes of console application for VintaSoft Imaging .NET SDK 12 can be downloaded from here.