Created
January 1, 2021 10:35
-
-
Save gofal/0aef12e9edd7c405b49534d608b7c0a5 to your computer and use it in GitHub Desktop.
Convert Dicom to Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void ConvertDicomToText() | |
{ | |
var dicomfile = Dicom.DicomFile.Open(@"D:\dicom\fo-dicom\gh-1083\dx.dcm"); | |
var output = new StringBuilder(); | |
var indentation = 0; | |
WriteDataset(dicomfile.Dataset, indentation, output); | |
System.IO.File.WriteAllText(@"D:\dicom\fo-dicom\gh-1083\dx.txt", output.ToString()); | |
} | |
private static void WriteDataset(DicomDataset ds, int indentation, StringBuilder output) | |
{ | |
foreach (var item in ds) | |
{ | |
output.Append(' ', indentation * 2); | |
output.Append(item.Tag); | |
if (item is DicomSequence sequence) | |
{ | |
output.AppendLine(); | |
for (int index = 1; index <= sequence.Items.Count; index ++) | |
{ | |
output.Append(' ', indentation * 2 + 2); | |
output.AppendLine("ITEM " + index.ToString()); | |
WriteDataset(sequence.Items[index - 1], indentation + 2, output); | |
} | |
} | |
else | |
{ | |
output.Append(": "); | |
output.AppendLine(ds.GetString(item.Tag)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment