0

gdmcgdcmPinvoke 有一个例外。为什么?

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";
        }
    }
}
4

1 回答 1

1

我建议不要为此任务使用任何 DICOM 阅读器,因为它会为该过程增加大量开销。在这种情况下使用完整 DICOM 库的唯一原因是,如果您想验证文件的所有元素,以及确保文件实际上是 DICOM 文件。

我的第一个建议是简单地依靠文件扩展名(通常是“.DCM”)来初步识别 DICOM 文件。然后,如果文件格式不正确,请在用户尝试打开文件时通知他们。我知道没有其他使用“.DCM”扩展名的文件格式。

如果这是不可接受的(例如您的文件没有扩展名),我只会为您的特定用例进行最低限度的验证。DICOM 文件将始终包含 128 字节的前导码,后跟字母“DICM”(不带引号)。你可以用你想要的任何东西来填充序言,但是字节 129-132 总是必须包含“DICM”。这是最低限度的文件验证,我建议如下:

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";
}
于 2010-09-08T18:37:46.423 回答