目前,我正在构建一个可以处理DICOM文件的小型桌面应用程序。我用C#和.NET编写代码,并使用ClearCanvas库。我需要做的一件事是能够显示文件的完整内容,包括所有序列。但是序列是以递归的方式完成的,所以每个序列中可以有更多的序列。现在,我的代码可以访问前两个级别,但我只是以测试人员的身份这样做,因为我需要能够访问第n个序列级别。因此,我需要以某种方式将其自动化。这就是我的代码在前两个级别中的样子。
DicomSequenceItem[] seq = attrs2[i].Values as DicomSequenceItem[];
if (seq != null)
{
for (int j = 0; j < seq.Length; j++)
{
for (int n = 0; n < seq[j].Count; n++)
{
DicomSequenceItem[] level2 = seq[j].ElementAt(n).Values as DicomSequenceItem[];
if(seq[j].ElementAt(n).GetValueType().ToString().Equals("ClearCanvas.Dicom.DicomSequenceItem"))
{
for (int k = 0; k < level2.Length; k++)
{
for (int l = 0; l < level2[k].Count; l++)
{
text += "\t\t" + level2[k].ElementAt(l) + "\r\n";
}
}
}
else
{
text += "\t" + seq[j].ElementAt(n) + "\r\n";
}
}
}
}任何帮助(代码样本)都将非常感谢。
谢谢!
发布于 2012-05-30 02:45:48
下面是一个简单的递归例程,用于遍历属性集合中的标记,包括递归遍历集合中可能存在的任何序列元素:
void Dump(DicomAttributeCollection collection, string prefix, StringBuilder sb)
{
foreach (DicomAttribute attribute in collection)
{
var attribSQ = attribute as DicomAttributeSQ;
if (attribSQ != null)
{
for (int i=0; i< attribSQ.Count; i++)
{
sb.AppendLine(prefix + "SQ Item: " + attribSQ.ToString());
DicomSequenceItem sqItem = attribSQ[i];
Dump(sqItem, prefix + "\t", sb);
}
}
else
{
sb.AppendLine(prefix + attribute.ToString());
}
}
}DicomAttributeCollection是可枚举的,因此您可以只使用foreach循环来遍历集合中的所有属性。属性本身存储在SortedDictionary中,因此它们在枚举时也会按标签升序排列。
请注意,如果下载了ClearCanvas库的源代码,还可以查看作为DicomAttributeCollection类一部分的真正的Dump()方法。它遍历集合并将集合中的所有标记写入StringBuilder实例。
https://stackoverflow.com/questions/10804130
复制相似问题