来自 MemoryStream 的 fo-dicom JPEG 图像压缩

fo-dicom JPEG Image Compression from MemoryStream

我正在寻找加载 dicom 图像并将其保存为 jpeg,如 fo-dicom 文档中的以下代码所示:

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

但是,我正在从 MemoryStream 打开 dicom 文件,我想将 jpeg 保存为字节数组。 fo-dicom 可以吗?

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;
}