Evil-DICOM
Evil-DICOM copied to clipboard
Collection was modified Exception on AnonymizationQueue.Anonymize(dcmFile)
My function is pretty similar to the example from the wiki. Wiki - Anonymization Operations
private static void Anonymize(string source, string name = "Sanitized", string id = "99999")
{
var toAnonymize = Directory.GetFiles(source);
var settings = AnonymizationSettings.Default;
//Change mapping but keep connections
settings.DoAnonymizeUIDs = false;
settings.FirstName = name;
settings.LastName = " ";
settings.Id = id;
//Gets a current list of UIDs so it can create new ones
var queue = EvilDICOM.Anonymization.AnonymizationQueue.BuildQueue(settings, toAnonymize);
for (int i = 0; i < toAnonymize.Length; i++)
{
Console.WriteLine("Anonymizing {0}", toAnonymize[i]);
var dcm = DICOMObject.Read(toAnonymize[i]);
queue.Anonymize(dcm);
//Write back to initial location - though this can be a different place
dcm.Write(toAnonymize[i]);
}
}
When I now execute the function it throws the following execption on queue.Anonymize(dcm);
Unhandled exception. System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List1.Enumerator.MoveNext() at System.Linq.Enumerable.WhereListIterator
1.MoveNext()
at EvilDICOM.Anonymization.Anonymizers.PrivateTagAnonymizer.Anonymize(DICOMObject d)
at EvilDICOM.Anonymization.AnonymizationQueue.Anonymize(DICOMObject dcm)
at DicomSanitzer.Program.Anonymize(String source, String name, String id) in C:\Users[WINDOWSUSER]\source\repos\Toolbox\DicomSanitzer\Program.cs:line 49
Hello. Same problem Did you find a solution ?
Hello. Same problem Did you find a solution ?
Hi, no unfortunately not. Did change the method to manually set the tags
I found the problem (for me anyway) and fixed it (I think) by updating EvilDICOM\Anonymization\Anonymizers\PrivateTagAnonymizer.cs as follows:
` //customized - fixed //foreach (var priv in d.AllElements.Where(e => e.Tag.IsPrivate())) // d.Remove(priv.Tag); d.AllElements.RemoveAll(x => x.Tag.IsPrivate());``
Sorry, I do not have time to contribute with pull request etc.