user1620150
user1620150

Reputation: 21

How to convert a DICOM file with no extension to a .png file or a .jpg file?

I have a simple C# Console Application that places a dicom file into a stream and then Copies that stream to a .jpg file. My code creates the file but I'm unable to view the image. Below is the code I am using.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using rzdcxLib;
using System.Drawing.Imaging;

namespace ConsoleApplication1
{
    class Program
    {
       public static void CopyStream(Stream input, Stream output) { 
            byte[] buffer = new byte[8 * 1024];
            int len;
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0) { 
                output.Write(buffer, 0, len); 
            } 
        } 
        static void Main(string[] args)
        {
            Stream myStream = 
             new FileStream("F:\\MIS\\JLoren\\Projects\\" + 
                             "814-Convert for Chandra\\DICOM\\Test\\IM1",
                            FileMode.Open, 
                            FileAccess.Read);
            Stream OutPut = 
             new FileStream("F:\\MIS\\JLoren\\Projects\\" +
                            "814-Convert for Chandra\\DICOM\\Test\\IM1.jpg", 
                            FileMode.Create, 
                            FileAccess.Write);
            CopyStream(myStream, OutPut);
        }
    }
}

Upvotes: 2

Views: 4565

Answers (1)

Ani
Ani

Reputation: 10896

Look here for information on the DICOM image format and how to read up DICOM images with C#. Note that these images are often 16 bit grayscale, which means a 8bpp (per-channel) monitor format cannot display these images with their full color range. You will have to choose a value of "window" and "level" for your particular conversion. Fixing these values might cause you to lose information in the converted image. See: Hounsfeld units.

Also - one of the core tenets of DICOM is that the image be lossless. Ensure that this is acceptable for your purpose before proceeding with a JPG conversion. PNG ought to be ok.

Upvotes: 4

Related Questions