tags:

views:

26

answers:

1

there's an exception with gdmcgdcmPinvoke. Why?

foreach (string el in files_in_folder)
                {
                    try
                    {

                    gdcm.ImageReader reader = new gdcm.ImageReader();
                        reader.SetFileName(el);

if (reader.Read())

{

  textBox1.Text="Image loaded"; 

  reader.GetImage() ;

 ListViewItem str = new ListViewItem(el);

 str.Text = el;

 listView1.Items.Add(str.Text);

}


                        else
                            textBox1.Text = "This is not a DICOM file";
                    }
}
A: 

I would suggest not using any DICOM Reader for this task, as it will add a significant overhead to the process. The only reason to use a full DICOM library in this instance is if you want to verify all the elements of the file, as well as ensuring that the file is in fact a DICOM file.

My first suggestion would be to simply rely on the file extension (usually ".DCM") to initially identify DICOM files. Then if the file is not in the correct format, inform the user when they try to open the file. I know of no other file formats that use the ".DCM" extension.

If that isn't acceptable (such as your files have no extension), I would only do the minimum validation required for your particular use case. A DICOM file will always contain a preamble of 128 bytes, followed by the letters "DICM" (without quotes). You can fill the preamble with anything you want, but bytes 129-132 always must contain the "DICM". That being the bare minimum file validation, I would suggest the following:

foreach (string el in files_in_folder)
{
    bool isDicomFile = false;
    using (FileStream fs = new FileStream(el, FileMode.Open))
    {
        byte[] b = new byte[4];
        fs.Read(b, 128, b.Length);
        ASCIIEncoding enc = new ASCIIEncoding();
        string verification = enc.GetString(b);
        if (verification == "DICM")
            isDicomFile = true;
        fs.Close();
    }
    if (isDicomFile)
        listView1.Items.Add(new ListViewItem(el));
    // I would discourage the use of this else, since even
    // if only one file in the list fails, the TextBox.Text
    // will still be set to "This is not a DICOM file".
    else
        textBox1.Text = "This is not a DICOM file";
}
md5sum
thanks for help.
luc