DICOM viewer with .NET

After an application interview at Zeiss Meditec I was given a homework with two excercise. The first is about parsing some patient data from xml files with DTD which is not really new stuff for me. The second excercise is an implementation of a DICOM file viewer for a given example VL dicom file. The task tells to use whether C++, Java or C# and because I didn’t do C# for a while I started looking for a good .NET dicom library.

Looking at some OpenSource projects: they’re horrible! Most of the libs are in a pre- alpha- testing- doNotUseInProductiveEnvironments phase and the example viewers of some libs could not read meditec’s VL dicom file at all. Quite promising appeared GrassRoots as it’s a C++ based lib that is wrapped to C#, Python, etc. using swig. But I encountered some issues and as the documentation is close to zero I continued my lib-search.

Finally with mDCM I found a quite easy-to-use .NET C# implementation which was very fast to integrate into my little viewer app. I used a Utility class to access the library methods using the Dicom.Data namespace. To open a dicom file I used:

bool success = false;
m_fileformat = new DicomFileFormat();
try {
m_fileformat.Load(filename, DicomReadOptions.Default |
DicomReadOptions.KeepGroupLengths |
DicomReadOptions.DeferLoadingLargeElements |

success = true;
catch (Exception e)
m_lasterror = "Error parsing file! : " + e.Message + "n";
return false;

return success;

Where m_fileformat is a Dicom.Data.DicomFileFormat data member. After that I read the pixel data to obtain the enclosed image:

DcmPixelData pixeldata = new DcmPixelData(m_fileformat.Dataset);
if (pixeldata.NumberOfFrames == 0)
m_lasterror = "No image data found" + "n";
return false;
else if (pixeldata.NumberOfFrames == 1) // currently only first img
MemoryStream strm = null;
strm = new MemoryStream(pixeldata.GetFrameDataU8(0));
img = System.Drawing.Image.FromStream(strm);
catch (Exception ex)
m_lasterror = "Error reading image from dicom file (" + ex.ToString() + ")" + "n";
return false;
return true;

As one can see, most likely only 8bit-per-channel images will be supported here (GetFrameDataU8() returns a plain byte[]). Moreover when testing with other sample dicom files this threw exceptions in many cases, so I guess I should look for another .NET lib or alternatively switch to Java or C++ 😦

=> zdicomviewer sourcecode and documentation


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s