VintaSoft Imaging .NET SDK 15.0: Documentation for .NET developer
In This Topic
    DICOM: How to convert a DICOM file to JPEG images?
    In This Topic
    Here is C# code that demonstrates how to convert a DICOM file to JPEG images:
    using System;
    using System.ComponentModel;
    using Vintasoft.Imaging;
    
    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;
                }
    
                try
                {
                    // convert a DICOM file to a JPEG files
                    ConvertDicomToJpeg(dicomFilePath);
                }
                catch (TypeInitializationException ex)
                {
                    if (ex.InnerException != null)
                    {
                        if (ex.InnerException.InnerException is LicenseException)
                        {
                            Console.WriteLine(ex.InnerException.InnerException.Message);
                            return;
                        }
                    }
                    throw ex;
                }
            }
    
            /// <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();
                }
            }
    
        }
    }