Extracting an Image from a DICOM file

When dealing with DICOM files, there are times when you need to include an image in a report or provide images to a patient so they can view the data without needing a DICOM-specific application. Thankfully, the LEADTOOLS Medical SDK makes it easy for you to create an application to extract an image from a DICOM file.

Below are some C# and Java code snippets to extract an image from a DICOM file.

C# code to extract image from a DICOM file


void ExtractDicomImage(string sourceFilePath)
{
    DicomEngine.Startup();
    using (DicomDataSet ds = new DicomDataSet())
    {
        ds.Load(sourceFilePath, DicomDataSetLoadFlags.None);

        DicomElement pixelDataElement = ds.FindFirstElement(null, DicomTag.PixelData, true);
        if (pixelDataElement == null)
        {
            Console.WriteLine("This dataset is missing the pixel data element");
            return;
        }

        if (ds.GetImageCount(pixelDataElement) == 0)
        {
            Console.WriteLine("Sample: This dataset has no images");
            return;
        }

        DicomImageInformation imageInformation = ds.GetImageInformation(pixelDataElement, 0);
        if (imageInformation == null)
        {
            Console.WriteLine("Sample: Can't retrieve image information");
            return;
        }

        RasterImage image = ds.GetImage(pixelDataElement, 0, 0, RasterByteOrder.Gray, DicomGetImageFlags.AllowRangeExpansion | DicomGetImageFlags.AutoApplyModalityLut | DicomGetImageFlags.AutoApplyVoiLut);

        if (image == null)
        {
            Console.WriteLine("Sample: Can't retrieve image");
            return;
        }

        using (RasterCodecs codecs = new RasterCodecs())
        {
            codecs.Save(image, "C:/LEADTOOLS22/Resources/Images/OutputDicomFile.jpg", RasterImageFormat.Jpeg, image.BitsPerPixel);
        }
    }

    DicomEngine.Shutdown();
}

Java code to extract image from a DICOM File


void ExtractDicomImage(String sourceFilePath)
{
    DicomEngine.startup();
    DicomDataSet ds = new DicomDataSet("C:/LEADTOOLS22/Resources/");
        
    ds.load(sourceFilePath, DicomDataSetLoadFlags.NONE);
    
    DicomElement pixelDataElement = ds.findFirstElement(null, DicomTag.PIXEL_DATA, true);
    if (pixelDataElement == null)
    {
        System.out.println("This dataset is missing the pixel data element");
        return;
    }
    
    if (ds.getImageCount(pixelDataElement) == 0)
    {
        System.out.println("Sample: this dataset has no images");
        return;
    }
    
    DicomImageInformation imageInformation = ds.getImageInformation(pixelDataElement, 0);
    if (imageInformation == null)
    {
        System.out.println("Sample: Can't retrieve image information");
        return;
    }
    
    RasterImage image = ds.getImage(pixelDataElement, 0, 0, DicomGetImageFlags.ALLOW_RANGE_EXPANSION | DicomGetImageFlags.AUTO_APPLY_MODALITY_LUT | DicomGetImageFlags.AUTO_APPLY_VOI_LUT);

    if (image == null)
    {
        System.out.println("Sample: Can't retrieve image");
        return;
    }
    
    RasterCodecs codecs = new RasterCodecs();
            
    codecs.save(image, "C:/LEADTOOLS22/Resources/Images/OutputDicomFile.jpg", RasterImageFormat.JPEG, image.getBitsPerPixel());
    
    codecs.dispose();
    ds.dispose();
    DicomEngine.shutdown();
}

Download for Free!

Our LEADTOOLS evaluation SDK is free to download and fully-functional for 60 days. Included with the evaluation is access to our support team via email and live chat.

But wait, there’s more!

Be sure to check out our online documentation library, full of comprehensive help files with demos and tutorials to help you get started.

For pricing or licensing questions, contact our sales team via email or call us at 704-332-5532.

About 

Developer Advocate

    Find more about me on:
  • linkedin
  • twitter
  • youtube
This entry was posted in Medical Imaging and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *