简体   繁体   English

在C#中解压缩JPEG图像的最快方法是什么?

[英]What's the fastest way to decompress JPEG images in C#

I need to write an app to playback a DICOM multiframe image. 我需要编写一个应用程序来播放DICOM多帧图像。 Each frame is stored in JPEG format. 每帧都以JPEG格式存储。 All the frames are stored consecutively in one file. 所有帧都连续存储在一个文件中。 Right now, I read out each frame data and pass it to the following routine to construct a Bitmap for display: 现在,我读出每个帧数据并将其传递给以下例程,以构建一个用于显示的位图:

    Bitmap CreateBitmap(byte[] pixelBuffer, int frameSize)
    {
        Bitmap image = null;

        try
        {
            long startTicks = DateTime.Now.Ticks;
            MemoryStream pixelStream = new MemoryStream(pixelBuffer, 0, frameSize);
            image = new Bitmap(pixelStream);
            loadTime = DateTime.Now.Ticks - startTicks;
        }
        catch (Exception ex)
        {
            Log.LogException(ex);
        }

        return image;
    }

During the test, everything works fine except that the performance in the above routine is not optimal. 在测试期间,除了上述例程中的性能不是最佳之外,一切正常。 For the 800x600 frame size, the time it takes in this routine is either 0msec and 15msec (I don't know why). 对于800x600帧大小,此例程所需的时间为0毫秒和15毫秒(我不知道为什么)。 For the 1024x768 frame size, the time it takes is either 15msec or 31msec. 对于1024x768帧大小,所需时间为15毫秒或31毫秒。 My goal is to stream the image data in and playback the image (1024x768 version) in 60Hz without dropping a frame. 我的目标是流式传输图像数据并以60Hz的频率播放图像(1024x768版本)而不丢帧。 That suggests I have to decompress the JPEG frame within 15msec constantly. 这表明我必须在15毫秒内不断地解压缩JPEG帧。 So my question is what's the better way to do this? 所以我的问题是更好的方法是什么?

It's 0 msec or 15 msec because your timer lacks resolution. 它是0毫秒或15毫秒,因为你的计时器缺乏分辨率。 Use QueryPerformanceCounter to get accurate times. 使用QueryPerformanceCounter获取准确的时间。

The WPF JPEG decoder ( System.Windows.Media.Imaging ) is faster than the GDI+ decoder. WPF JPEG解码器( System.Windows.Media.Imaging )比GDI +解码器更快。

I realize you probably have constraints that dictate the JPEG-frame requirement, but the best way to do this is to use a different format designed for video, like MEPG or Quicktime. 我意识到你可能有限制规定JPEG帧要求,但最好的方法是使用为视频设计的不同格式,如MEPG或Quicktime。 Translate your JPEG frames into a video format designed for streaming and stream that, instead. 将您的JPEG帧转换为专为流式传输和流式传输而设计的视频格式。

Ok, I figured out how to use System.Windows.Media.Imaging.JpegBitmapEncoder in the method now: 好的,我想出了如何在方法中使用System.Windows.Media.Imaging.JpegBitmapEncoder:

            JpegBitmapDecoder decoder = new JpegBitmapDecoder(pixelStream, BitmapCreateOptions.None, BitmapCacheOption.None);
            BitmapFrame frame = decoder.Frames[0];
            frame.CopyPixels(pixelBuffer, stride, 0);

I measured the performance and it seems to have 100% improvement. 我测量了性能,似乎有100%的改进。 Nice job from WIC and WPF. 来自WIC和WPF的不错的工作。 Decoding 1024x768 image is down to 9 to 10 msec now. 现在将1024x768图像解码到9到10毫秒。

You could try WIC, the windows imaging component. 您可以尝试WIC,即Windows成像组件。 This is probably used behind the scenes by WPF, suggested in another answer. 这可能是WPF在幕后使用的,在另一个答案中提出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM