tags:

views:

875

answers:

5

I would like to read a DICOM file in C#. I don't want to do anything fancy, I just for now would like to know how to read in the elements, but first I would actually like to know how to read the header to see if is a valid DICOM file.

It consists of Binary Data Elements. The first 128 bytes are unused (set to zero), followed by the string 'DICM'. This is followed by header information, which is organized into groups.

A sample DICOM header

First 128 bytes: unused DICOM format.
Followed by the characters 'D','I','C','M'
Followed by extra header information such as:

0002,0000, File Meta Elements Groups Len: 132
0002,0001, File Meta Info Version: 256
0002,0010, Transfer Syntax UID: 1.2.840.10008.1.2.1.
0008,0000, Identifying Group Length: 152
0008,0060, Modality: MR
0008,0070, Manufacturer: MRIcro

In the above example, the header is organized into groups. The group 0002 hex is the file meta information group which contains 3 elements: one defines the group length, one stores the file version and the their stores the transfer syntax.

Questions

  • How to I read the header file and verify if it is a DICOM file by checking for the 'D','I','C','M' characters after the 128 byte preamble?
  • How do I continue to parse the file reading the other parts of the data?
+3  A: 

A quick Google search brought up three DICOM C# libraries:

Bauer
Libraries are good, but I am trying to do it myself just for learning purposes. I just need a starting point on how to read the files... Thanks for the links
Xaisoft
+1  A: 

Here is a sample in C#: http://www.codeproject.com/KB/graphics/dicomImageViewer.aspx

Sameh Serag
The link was very helpful. Thanks.
Xaisoft
+1  A: 

Something like this should read the file, its basic and doesn't handle all cases, but it would be a starting point:


public void ReadFile(string filename)
{
    using (FileStream fs = File.OpenRead(filename))
    {
        fs.Seek(128, SeekOrigin.Begin);
        if (!(fs.ReadByte() != (byte)'D' ||
              fs.ReadByte() != (byte)'I' ||
              fs.ReadByte() != (byte)'C' ||
              fs.ReadByte() != (byte)'M'))
        {
            Console.WriteLine("Not a DCM");
            return;
        }
        BinaryReader reader = new BinaryReader(fs);

        ushort g;
        ushort e;
        do
        {
            g = reader.ReadUInt16();
            e = reader.ReadUInt16();

            string vr = new string(reader.ReadChars(2));
            long length;
            if (vr.Equals("AE") || vr.Equals("AS") || vr.Equals("AT")
                || vr.Equals("CS") || vr.Equals("DA") || vr.Equals("DS")
                || vr.Equals("DT") || vr.Equals("FL") || vr.Equals("FD")
                || vr.Equals("IS") || vr.Equals("LO") || vr.Equals("PN")
                || vr.Equals("SH") || vr.Equals("SL") || vr.Equals("SS")
                || vr.Equals("ST") || vr.Equals("TM") || vr.Equals("UI")
                || vr.Equals("UL") || vr.Equals("US"))
               length = reader.ReadUInt16();
            else
            {
                // Read the reserved byte
                reader.ReadUInt16();
                length = reader.ReadUInt32();
            }

            byte[] val = reader.ReadBytes((int) length);

        } while (g == 2);

        fs.Close();
    }

    return ;
}

The code does not actually try and take into account that the transfer syntax of the encoded data can change after the group 2 elements, it also doesn't try and do anything with the actual values read in.

Steve Wranovsky
Thanks Steve, I noticed you are on a lot of posts about DICOM especially regarding ClearCanvas. I respect your answer. Thanks again.
Xaisoft
Thanks, Xaisoft. If you really want to look, I copied some of this code from the ClearCanvas libraries, which are based on an early version of mDCM. You can look at the ClearCanvas.Dicom.IO.DicomStreamReader class to see our parser, which is obviously a lot more complex than just this snippet.
Steve Wranovsky
+1  A: 

Just some pseudologic

How to I read the header file and verify if it is a DICOM file by checking for the 'D','I','C','M' characters after the 128 byte preamble?

  • Open as binary file, using File.OpenRead
  • Seek to position 128 and read 4 bytes into the array and compare it againts byte[] value for DICM. You can use ASCIIEncoding.GetBytes() for that

How do I continue to parse the file reading the other parts of the data?

  • Continue reading the file using Read or ReadByte using the FileStream object handle that you have earlier
  • Use the same method like above to do your comparison.

Dont forget to close and dispose the file.

Fadrian Sudaman
Thanks for the Psudocode, this helps.
Xaisoft
A: 

hi, you can also use like this.

FileStream fs = File.OpenRead(path);

byte[] data = new byte[132];
fs.Read(data, 0, data.Length);

int b0 = data[0] & 255, b1 = data[1] & 255, b2 = data[2] & 255, b3 = data[3] & 255;

if (data[128] == 68 && data[129] == 73 && data[130] == 67 && data[131] == 77)
        {
           //dicom file
        }
        else if ((b0 == 8 || b0 == 2) && b1 == 0 && b3 == 0)
        {
            //dicom file
        }
prashant