【问题标题】:Prevent C# WPF BitmapSource bytes from being copied before render防止在渲染之前复制 C# WPF BitmapSource 字节
【发布时间】:2016-07-08 05:20:23
【问题描述】:

我一直在使用 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 上。

【问题讨论】:

    标签: c# .net wpf memory bitmap


    【解决方案1】:

    我无法在渲染前阻止复制。在尝试了从 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; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多