Copying DicomItems from one file to another without checking the type of each

37 views
Skip to first unread message

James Allsopp

unread,
Jul 27, 2016, 4:57:17 AM7/27/16
to Fellow Oak DICOM
Hi,
 I'm looping through the attributes in one file and trying to write them to another file. I could solve the problem for some attribute types with a line like;
            foreach (DicomItem attribute in file1.Dataset)
            {
                try
                {
                    file.Dataset.Add(attribute.Tag,attribute.ToString());
                }
                catch (Exception e)
                {
                    Debug.WriteLine(e);
                }
            }

However a lot of lines throw exceptions. Is there a reliable technique for handling this so I can just copy the attribute from one file into another without having to test each value, find its type and cast appropriately. I'm not sure how to handle the generic T that the Add method has,

Thanks,
James

Anders Gustafsson Cureos AB

unread,
Aug 5, 2016, 5:20:40 AM8/5/16
to Fellow Oak DICOM
Hi James,

are you copying a few items or (practically) the entire file? If you are copying (most) of the file, you could use the Clone extension method:

var newFile = oldFile.Clone();

You might also want to look into the code of the Clone extension method here: https://github.com/fo-dicom/fo-dicom/blob/development/DICOM/DicomFileExtensions.cs
to get some inspiration of how you can proceed if the Clone method is not sufficient for you.

Regards,
Anders @ Cureos
Reply all
Reply to author
Forward
0 new messages