DICOM communication in C# with fo-dicom library

Recently I have been working with a client on a DICOM protocol and communication. In order to implement the required DICOM features I have first looked up the options of the DICOM libraries in the .NET and C# world. The best library I have found is the fo-dicom library (https://github.com/fo-dicom/fo-dicom) and I am going to share some thoughts on that and describe a couple of non-trivial examples.

The library is open source, but it it not that straight forward to include in your project (however it is possible for sure) and build as the project is multi-platform and I was hitting some issues when trying to integrate the code. It is however available as NuGet package and in my case of WPF application I went for the Desktop version of the NuGet.

These are the DICOM features I implemented using this library:

  • C-ECHO
  • C-FIND (MWL query)
  • C-STORE

The good think about the library is that it has live discussion forum at Gitter where authors and other contributors respond within hours.

Using fo-dicom calls in await/async fashion

Because all the DICOM calls using the fo-dicom library are done in an asynchronous fashion (which makes perfect sense), but using the events fired when the request status changes, we need to use a thread synchronization patterns in order to write our code in the async/await fashion. This is how the basic DICOM call looks like:

This way we could let the event bubble up in the application, however the way I wanted to implement this was in a service library with async/await methods. So we can change slightly by adding the ManualResetEvent thread synchronization class like this and set it in the event. This will return the control back to the calling thread while waiting for the response to complete and hence will not block the calling thread:

The code will process until the wait.WaitOneAsync(). Then await until the wait.Set() is called in the event callback, which will allow the awaited code to complete and continue.

Processing list of results

When processing a list of responses from for example a C-FIND query you need to take a look at the status of each response event. The way multiple items (let’s say patients from the search query) are returned are not in a single response containing multiple items, but instead for each item (patient) a callback event is fired with the “Pending” status of the response. When the response status is “Succeed” this means the communication can be closed. When getting a Pending response this means we are getting one item of many and further responses are expected and should be processed. We can handle the responses like this:

I was also implementing a testing DICOM server (PACS) which returns C-FIND responses and was having issues with the returned data not being processed until I realized that each of the result items needs to be returned with the “Pending” status and the final one needs to have the “Success” status;)

Send a Comment

Your email address will not be published.