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";
}
}
}
答案 0 :(得分:1)
我建议不要使用任何DICOM Reader来执行此任务,因为这会给流程增加很多开销。在此实例中使用完整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";
}