Search code examples
c#fo-dicom

fo-Dicom - How do I extract image frames from the DicomFile


I need to extract all image frames from a DICOM SC using fo-DICOM. I have a test app that extracts and displays the images, which works fine. However, I need to save the individual images to a database, and am running into problems.

I have the following code so far:

    public void SetImages(DicomFile dicom, ThumbResults results)
    {
        var images = new DicomImage(dicom.Dataset);
        
        for(var count = 0; count < images.NumberOfFrames; count++)
        {
            var image = images.RenderImage(count).As<Bitmap>();
            using(var stream = new MemoryStream())
            {
                image.Save(stream, ImageFormat.Jpeg);
                results.Images.Add(Convert.ToBase64String(stream.ToArray()));
            }
        }
    }

I get a DicomImagingException, "Cannot cast to 'Bitmap'" on images.RenderImage. It works in my test code, when I call PictureBox.Image = _image.RenderImage(count).As<Bitmap>(); so I figure RenderImage must be specifically for rendering (as the name implies).

How should I go about extracting individual frames to a string that will be saved to the database?


Solution

  • In case someone else runs into this problem, the issue was the original code was in .NET Framework, but the new code was in .NET Core. In Core, the ImageManager does not use the WindowsImageManager by default, so you need to set it manually.

    ImageManager.SetImplementation(WinFormsImageManager.Instance);