2
votes

I am looking to load a dicom image and save it to a jpeg, as seen in the below code from fo-dicom documentation:

var image = new DicomImage(@"test.dcm");
image.RenderImage().AsBitmap().Save(@"test.jpg");      

However, I am opening the dicom file from a MemoryStream, and I would like to save the jpeg as a byte array. Is this possible with fo-dicom?

1

1 Answers

4
votes
private byte[] JpegBytesFromDicomStream(Stream stream)
{
    byte[] result;

    // Open image from Stream using DicomFile class
    DicomFile file = DicomFile.Open(stream);
    DicomImage image = new DicomImage(file.Dataset);

    using (IImage renderedImage = image.RenderImage())
    {
        Bitmap bitmap = renderedImage.AsSharedBitmap();

        // Copy image to byte array using MemoryStream
        using (MemoryStream targetStream = new MemoryStream())
        {
            bitmap.Save(targetStream, ImageFormat.Jpeg);
            result = targetStream.ToArray();
        }
    }

    return result;
}