Search code examples
unit-testing.net-coreembedded-resourcefo-dicom

Cannot access DICOM image stream when loading from a resource


I am using fo-Dicom to access image streams. I have unit tests that call a method to load test DICOM files. To automate the process in the build pipeline, I switched from using the image path to loading a resource. I now load my test DICOM file like so:

    public static DicomFile RequestDicomFile(Resource resource)
    {
        //return DicomFile.Open(Reference.GetPath(resource));

        DicomFile result = null;
        var assembly = Assembly.GetExecutingAssembly();

        using (Stream stream = assembly.GetManifestResourceStream($"CommonTesting.Resources.{resource.ToString()}.dcm"))
        {
            result = DicomFile.Open(stream);
        }

        return result;
    }

This returns a DicomFile, and I am able to access the associated tags. However, when I load the image using the (abbreviated) code below, I get an exception saying "cannot read from stream - maybe closed" on the RenderImage line.

        DicomImage images; = new DicomImage(dicom.Dataset);
        if (images.NumberOfFrames > 0)
        {
            var imageData = images.RenderImage(0).As<Bitmap>();
            ...
        }

When I open the DicomFile from a file path (the commented out code in RequestDicomFile, I am able to access the rendered images.

Does anyone know why this would happen with a Resource file? Alternately, is there a better way to reference my test files without trying to copy them to the correct path in the Azure pipeline?


Solution

  • The solution was to fully read the stream when opening the DICOM file.

    result = DicomFile.Open(stream, FileReadOption.ReadAll);