Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF图像多源逐个更改导致内存泄漏_Wpf_Image - Fatal编程技术网

WPF图像多源逐个更改导致内存泄漏

WPF图像多源逐个更改导致内存泄漏,wpf,image,Wpf,Image,我编写了一个类,该类继承图像并在初始化该类时初始化图像源数组 触发渲染时,有如下代码:调试后,我发现 this.Source = _listOfImageSource[_currentIamgeIndex]; 这将导致内存泄漏 但我不知道如何纠正这一点?如何在图像源更改之前释放内存 有什么评论吗 public void CompositionTarget_Rendering(object sender, EventArgs e) { GC.Collect();

我编写了一个类,该类继承图像并在初始化该类时初始化图像源数组

触发渲染时,有如下代码:调试后,我发现

this.Source = _listOfImageSource[_currentIamgeIndex];
这将导致内存泄漏

但我不知道如何纠正这一点?如何在图像源更改之前释放内存

有什么评论吗

public void CompositionTarget_Rendering(object sender, EventArgs e)
    {
        GC.Collect();
        GC.WaitForPendingFinalizers();
        if ((_listOfImageSource.Count > 0) &&
            _currentIamgeIndex < _listOfImageSource.Count)
        {

            this.Source = null;
            this.Source = _listOfImageSource[_currentIamgeIndex];    
        }
        _currentIamgeIndex += _stepInterval;

        if (_currentIamgeIndex > _listOfImageSource.Count) //stop the animmation
        {

            this.Visibility = Visibility.Hidden;
            _parentWindow.Visibility = Visibility.Hidden;
            this.Source = null;
            CompositionTarget.Rendering -= CompositionTarget_Rendering;

        }
    }
public void CompositionTarget_呈现(对象发送方,事件参数e)
{
GC.Collect();
GC.WaitForPendingFinalizers();
如果(_listOfImageSource.Count>0)&&
_currentIamgeIndex<\u listOfImageSource.Count)
{
this.Source=null;
this.Source=_listOfImageSource[_currentIamgeIndex];
}
_currentIamgeIndex+=\u步距;
如果(\u currentIamgeIndex>\u listOfImageSource.Count)//停止动画
{
this.Visibility=Visibility.Hidden;
_parentWindow.Visibility=Visibility.Hidden;
this.Source=null;
CompositionTarget.Rendering-=CompositionTarget\u Rendering;
}
}

确保
\u图像源列表
集合中的所有图像都已冻结。这将减少内存消耗。请注意,在分配另一个值之前,没有必要将Source设置为null。这不会“释放”任何内容。是的,我在创建时对图像进行了预处理。var bitmapBingo=new BitmapImage(new Uri(pathBingo,UriKind.Absolute));bitmappbingo.Freeze()_listOfImageSource[0]。添加(bitmapBingo);