WPF中BitmapImage解码速度性能

7
我有5张图片,它们的像素高度和宽度都相同(2481 * 3508)。但是,其中一张是gif格式,一张是jpeg格式,一张是png格式,一张是bmp格式。现在我使用BitmapSource将它们渲染成图像,其中(1)使用原始像素高度的三分之二作为DecodePixelHeight,(2)使用原始像素高度作为DecodePixelHeight。
第一种情况:
bitmapImage.BeginInit();
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.DecodePixelHeight = 2/3 * originalHeight;
bitmapImage.StreamSource = streamWithTheFile;
bitmapImage.EndInit();
bitmapImage.Freeze();

BMP和Jpeg同样缓慢。Png和Gif所需时间不到一半。为什么?

第二种情况:

bitmapImage.BeginInit();
bitmapImage.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = streamWithTheFile;
bitmapImage.EndInit();
bitmapImage.Freeze();

在此之前,PNG所需时间的一半。JPEG和BMP所需时间的五分之一。GIF与以前一样需要相同的时间。

根据文档,我原本认为PNG和JPEG的性能与实际解码大小更无关,而其他格式则不然。为什么会这样呢?


好问题 - 有人知道为什么BitmapImage会表现出那样的行为吗? - alexander.biskop
2
RenderOptions.BitmapScalingMode属性(应用于实际显示图像的任何控件,通常是XAML <Image>元素)能否提高速度?我认为在.NET 3.5中默认设置为HighQuality,在.NET 4中默认设置为LowQuality,但两者都会在渲染速度方面产生某种程度的惩罚... - Marko
我正在开发一个应用程序,可以连续播放成千上万张图片(jpg或bmp格式),我试图优化播放速度。一方面,位图较大,从磁盘或内存缓存中加载需要更长时间,但它们不需要解码。另一方面,对于jpg格式的图片,情况恰好相反——解码比较慢,但从磁盘加载很快。最终我选择了图片的原始格式(jpg) ,因为这样需要的初始工作更少。重点是,缓存和磁盘加载可能会干扰时序。 - Dean
1个回答

0

我实现了一种性能最优的行为。 我将其用于从家庭安全摄像机实时传输视频,使用相当大的图像非常顺畅。

尝试一下并告诉我你的想法。 使用相当简单,分配依赖属性,将行为附加到图像上即可。 干杯。

注意:像素可以是IList,但由于C#数组实现了IList,因此您也可以分配数组。

注意2:不要分配图像源,因为这会覆盖行为的分配,只需绑定到行为的 Pixels 依赖属性即可。

public class VideoBehavior : Behavior<Image>
{

    public static readonly DependencyProperty PixelsProperty = DependencyProperty.Register(
        "Pixels", typeof (IList<byte>), typeof (VideoBehavior), new PropertyMetadata(default(IList<byte>),OnPixelsChanged));

    private static void OnPixelsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (VideoBehavior) d;
        var pixels = (IList<byte>) e.NewValue;


        b.RenderPixels(pixels);
    }


    public IList<byte> Pixels
    {
        get { return (IList<byte>) GetValue(PixelsProperty); }
        set { SetValue(PixelsProperty, value); }
    }

    public static readonly DependencyProperty PixelFormatProperty = DependencyProperty.Register(
        "PixelFormat", typeof (PixelFormat), typeof (VideoBehavior), new PropertyMetadata(PixelFormats.Default,OnPixelFormatChanged));


    private static void OnPixelFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (VideoBehavior) d;
        var pixelFormat = (PixelFormat) e.NewValue;

        if(pixelFormat==PixelFormats.Default)
            return;

        b._pixelFormat = pixelFormat;

        b.InitializeBufferIfAttached();
    }

    public PixelFormat PixelFormat
    {
        get { return (PixelFormat) GetValue(PixelFormatProperty); }
        set { SetValue(PixelFormatProperty, value); }
    }

    public static readonly DependencyProperty PixelWidthProperty = DependencyProperty.Register(
        "PixelWidth", typeof (int), typeof (VideoBehavior), new PropertyMetadata(default(int),OnPixelWidthChanged));

    private static void OnPixelWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (VideoBehavior)d;
        var value = (int)e.NewValue;

        if(value<=0)
            return;

        b._pixelWidth = value;

        b.InitializeBufferIfAttached();
    }

    public int PixelWidth
    {
        get { return (int) GetValue(PixelWidthProperty); }
        set { SetValue(PixelWidthProperty, value); }
    }


    public static readonly DependencyProperty PixelHeightProperty = DependencyProperty.Register(
        "PixelHeight", typeof (int), typeof (VideoBehavior), new PropertyMetadata(default(int),OnPixelHeightChanged));

    private static void OnPixelHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (VideoBehavior)d;
        var value = (int)e.NewValue;

        if (value <= 0)
            return;


        b._pixelHeight = value;

        b.InitializeBufferIfAttached();
    }

    public int PixelHeight
    {
        get { return (int) GetValue(PixelHeightProperty); }
        set { SetValue(PixelHeightProperty, value); }
    }

    public static readonly DependencyProperty DpiXProperty = DependencyProperty.Register(
        "DpiX", typeof (int), typeof (VideoBehavior), new PropertyMetadata(96,OnDpiXChanged));

    private static void OnDpiXChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (VideoBehavior)d;
        var value = (int)e.NewValue;

        if (value <= 0)
            return;


        b._dpiX = value;

        b.InitializeBufferIfAttached();
    }

    public int DpiX
    {
        get { return (int) GetValue(DpiXProperty); }
        set { SetValue(DpiXProperty, value); }
    }

    public static readonly DependencyProperty DpiYProperty = DependencyProperty.Register(
        "DpiY", typeof (int), typeof (VideoBehavior), new PropertyMetadata(96,OnDpiYChanged));

    private static void OnDpiYChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var b = (VideoBehavior)d;
        var value = (int)e.NewValue;

        if (value <= 0)
            return;


        b._dpiY = value;

        b.InitializeBufferIfAttached();
    }

    public int DpiY
    {
        get { return (int) GetValue(DpiYProperty); }
        set { SetValue(DpiYProperty, value); }
    }

    [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory")]
    public static extern void CopyMemory(IntPtr destination, IntPtr source, uint length);

    private IntPtr _backBuffer = IntPtr.Zero;
    private int _bytesPerPixel;
    private const int BitsPerByte = 8;
    private int _pixelWidth;
    private int _pixelHeight;
    private int _dpiX;
    private int _dpiY;
    private PixelFormat _pixelFormat;
    private Int32Rect _rect;

    private uint _byteArraySize;
    private WriteableBitmap _bitMap;

    private bool _attached;

    protected override void OnAttached()
    {
        _attached = true;
        InitializeBufferIfAttached();
    }

    private void InitializeBufferIfAttached()
    {
        if(_attached==false)
            return;

        ReevaluateBitsPerPixel();

        RecomputeByteArraySize();

        ReinitializeImageSource();
    }

    private void ReevaluateBitsPerPixel()
    {
        if(_pixelFormat==PixelFormats.Default)
            return;

        _bytesPerPixel = _pixelFormat.BitsPerPixel/BitsPerByte;
    }

    private void ReinitializeImageSource()
    {
        if(_pixelHeight<=0|| _pixelHeight<=0)
            return;

        _bitMap = new WriteableBitmap(_pixelWidth, _pixelHeight, _dpiX, _dpiY, _pixelFormat, null);
        _backBuffer = _bitMap.BackBuffer;
        _rect = new Int32Rect(0, 0, _pixelWidth, _pixelHeight);
        AssociatedObject.Source = _bitMap;
    }

    private async void RenderPixels(IList<byte> pixels)
    {
        if (_backBuffer == IntPtr.Zero)
            return;

        if (pixels == null)
        {
            return;
        }

        await Task.Factory.StartNew(() =>
        {
            var h = new GCHandle();
            var allocated = false;

            try
            {
                h = GCHandle.Alloc(pixels, GCHandleType.Pinned);
                allocated = true;
                var ptr = h.AddrOfPinnedObject();
                CopyMemory(_backBuffer, ptr, _byteArraySize);
            }
            finally
            {
                if (allocated)
                    h.Free();
            }
        });

        _bitMap.Lock();

        _bitMap.AddDirtyRect(_rect);
        _bitMap.Unlock();
    }

    private void RecomputeByteArraySize()
    {
        _byteArraySize = (uint)(_pixelWidth * _pixelHeight * _bytesPerPixel);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接