防止在呈现

时间:2016-03-21 15:39:55

标签: c# .net wpf memory bitmap

我一直在使用32位C#WPF应用程序,它在ListBox中显示大量大图像(在很多情况下为1080p)。问题是在我的C#对象(我已经绑定)中保留一个BitmapSource对象会大大增加内存,因为我创建的BitmapSource的字节在渲染之前被复制/复制。如果我保留BitmapSource对象以便重用它或在其他地方重新显示它,那么由于渲染前复制,我最终得到了原始图像字节的多个副本。更具体地说,在渲染之前调用CopyPixels(Int32Rect sourceRect, Array pixels, int stride, int offset)。带有堆栈跟踪的内存/堆分析确认了在渲染之前复制字节的想法。

唯一的"解决方法"我创建了,每次需要时生成BitmapSource 如下:

ImageData data = _backendImage.getData();
SWIGTYPE_p_unsigned_char rawData = _backendImage.getRawData();
IntPtr dataPointer = SWIGTYPE_p_unsigned_char.getCPtr(rawData).Handle;
GC.Collect(); // forces garbage collection on not-displayed images
return Utilities.GenerateBitmapSource((int)_backendImage.getWidth(), (int)_backendImage.getHeight(), _backendImage.getByteOrder(), dataPointer, data.Count);

最后一行有我自己的函数来实际生成一个BitmapSource对象,这超出了这个问题的范围。

解决方法在性能方面极差,因为在每次渲染到ListBox之前,我不只是一个,而是两个数据副本(一个进入BitmapSource,一个进行渲染)。保持BitmapSource可以删除所有重复的复制操作,但是内存使用非常繁重。

这是我的ListBox XAML:

<ListBox Name="SlideShowListBox" ItemsSource="{Binding SlideData.PreviewData}" 
                 SelectedIndex="{Binding SelectedIndex}" 
                 ScrollViewer.VerticalScrollBarVisibility="Visible"
                 SelectionMode="Extended"
                 VirtualizingStackPanel.VirtualizationMode="Recycling">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <VirtualizingStackPanel Orientation="Horizontal" Margin="0, 2.5, 0, 2.5">
                <Label Content="{Binding data.ImageNumber}" VerticalAlignment="Top" Width="30" HorizontalContentAlignment="Right" Margin="0,-6.5,0,0"/>
                <Grid>
                    <Image Source="{Binding data.ImageThumbnail}" RenderOptions.BitmapScalingMode="HighQuality" VerticalAlignment="Top" HorizontalAlignment="Left"
                           Name="ListImage"
                           MaxWidth="{Binding ElementName=ListBoxItemSizer,
                                            Path=ActualWidth, Converter={ikriv:MathConverter}, ConverterParameter=(x - 20)}">
                        <Image.Style>
                            <Style TargetType="{x:Type Image}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding data.IsHidden}" Value="True">
                                        <Setter Property="Opacity" Value="0.5"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>
                </Grid>
            </VirtualizingStackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

问题:当我已经将所有字节存储在RAM中并在图像上调用.Freeze()时,有没有办法阻止WPF在渲染之前复制字节?我想要我的图像字节的一个副本在RAM中:不多也不少。

可能相关:.NET Memory issues loading ~40 images, memory not reclaimed - 似乎无关,因为我正在从原始字节构建BitmapSource对象,而不是(文字)流对象。

编辑:有趣的澄清 - 我在两个不同的屏幕上显示2个不同ListBox项目中的这些BitmapSource项目。如果我保留对象,则RAM使用率仅在BitmapSource的第一次渲染时增加,而不是在后续渲染时增加,无论BitmapSource出现在哪个屏幕或ListBox上。

1 个答案:

答案 0 :(得分:0)

我无法阻止渲染前的副本。尝试从BitmapImageCacheOption = BitmapCacheOption.None到从文件而不是内存中的图像加载所有内容后,在RAM中保留1个字节副本的修复相对简单。

要修复它,请创建自己的继承自BitmapSource的自定义类。按照接受的答案here中的代码,根据您自己的图片格式进行必要的调整。例如,我需要使用自己的步幅值而不是提供的值,因为我将24bpp数组转换为Pbgra32格式。我使用不安全的复制代码来获得更快的副本(同样,根据我的用例进行了修改)。我已将我的代码复制到此帖的底部,但它与链接的SO帖子非常相似。

但是,您的自定义BitmapSource仍然有2个字节副本。 (CopyPixels函数名称赋予了它。)为了摆脱现在无关的副本,只需设置_data = null并让GC尽可能地清理它。田田! RAM中的一个字节副本,性能快,ListBox滚动工作,您可以在其他屏幕和其他地方重用BitmapSource,并且可以使用内存。

我很担心如果在渲染后调用它会中断CreateInstanceCore(),这可能会在我自己以外的其他用例中中断。

class RGB24BitmapSource : BitmapSource
{
    private byte[] _data;
    private int _stride;
    private int _pixelWidth;
    private int _pixelHeight;

    public RGB24BitmapSource(int pixelWidth, int pixelHeight, IntPtr data, int dataLength, int stride)
    {
        if (dataLength != 0 && data != null && data.ToInt64() != 0)
        {
            _data = new byte[dataLength];
            Marshal.Copy(data, _data, 0, dataLength);
        }
        _stride = stride;
        _pixelWidth = pixelWidth;
        _pixelHeight = pixelHeight;
    }

    private RGB24BitmapSource(int pixelWidth, int pixelHeight, byte[] data, int stride)
    {
        _data = data;
        _stride = stride;
        _pixelWidth = pixelWidth;
        _pixelHeight = pixelHeight;
    }

    unsafe public override void CopyPixels(Int32Rect sourceRect, Array pixels, int stride, int offset)
    {
        if (_data != null)
        {
            fixed (byte* source = _data, destination = (byte[])pixels)
            {
                byte* dstPtr = destination + offset;
                for (int y = sourceRect.Y; y < sourceRect.Y + sourceRect.Height; y++)
                {
                    for (int x = sourceRect.X; x < sourceRect.X + sourceRect.Width; x++)
                    {
                        byte* srcPtr = source + _stride * y + 3 * x;
                        byte a = 255;
                        *(dstPtr++) = (byte)((*(srcPtr + 2)) * a / 256);
                        *(dstPtr++) = (byte)((*(srcPtr + 1)) * a / 256);
                        *(dstPtr++) = (byte)((*(srcPtr + 0)) * a / 256);
                        *(dstPtr++) = a;
                    }
                }
            }
        }
        _data = null; // it was copied for render, so next GC cycle could theoretically reclaim this memory. This is the magic fix.
    }

    protected override Freezable CreateInstanceCore()
    {
        return new RGB24BitmapSource(_pixelWidth, _pixelHeight, _data, _stride);
    }

    // DO. NOT. COMMENT. THESE. OUT. IF YOU DO, CRASHES HAPPEN!
#pragma warning disable 0067 // disable unused warnings
    public override event EventHandler<DownloadProgressEventArgs> DownloadProgress;
    public override event EventHandler DownloadCompleted;
    public override event EventHandler<ExceptionEventArgs> DownloadFailed;
    public override event EventHandler<ExceptionEventArgs> DecodeFailed;
#pragma warning restore 0067

    public override double DpiX
    {
        get { return 96; }
    }

    public override double DpiY
    {
        get { return 96; }
    }

    public override System.Windows.Media.PixelFormat Format
    {
        get { return PixelFormats.Pbgra32; }
    }

    public override BitmapPalette Palette
    {
        get { return BitmapPalettes.WebPalette; }
    }

    public override int PixelWidth
    {
        get { return _pixelWidth; }
    }

    public override int PixelHeight
    {
        get { return _pixelHeight; }
    }

    public override double Width
    {
        get { return _pixelWidth; }
    }

    public override double Height
    {
        get { return _pixelHeight; }
    }
}
相关问题