Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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 ListBox加载了大量图片。PresentationCore.dll中是否会出现System.IO.IOException?_Wpf_Windows_Listbox - Fatal编程技术网

WPF ListBox加载了大量图片。PresentationCore.dll中是否会出现System.IO.IOException?

WPF ListBox加载了大量图片。PresentationCore.dll中是否会出现System.IO.IOException?,wpf,windows,listbox,Wpf,Windows,Listbox,我的项目模板非常简单,一个文本块显示名称,另一个图像显示网络图像: <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/>

我的项目模板非常简单,一个
文本块
显示名称,另一个
图像
显示网络图像

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Image VerticalAlignment="Center" Source="{Binding Url, Mode=OneWay, Converter={StaticResource cvtImage}}" Width="30" Height="30"/>
            <TextBlock Grid.Column="1" Text="{Binding Name, Mode=OneWay}" Margin="5" VerticalAlignment="Center"/>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>
AFET设置
lb.ItemsSource=this.imagesList,,
当您向下滑动列表框时,肯定会得到
IOException


我想这是关于内存泄漏的问题

我使用以下代码捕获异常:

try 
{
    var c = new WebClient();
    var bytes = c.DownloadData(url);
    var ms = new MemoryStream(bytes);

    var bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

    return bi;  
}
catch (Exception ex)
{
    Console.WriteLine("ex message: {0}", ex.Message);
    return new BitmapImage();
}
现在我可以得到异常,但仍然无法显示图像
https://p.qlogo.cn/gh/170883246/170883246_1/40

图像可以显示在WebBrowser中。我不知道为什么DotNet
BitmapImage
不能使用它。

我使用此代码捕获异常:

try 
{
    var c = new WebClient();
    var bytes = c.DownloadData(url);
    var ms = new MemoryStream(bytes);

    var bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

    return bi;  
}
catch (Exception ex)
{
    Console.WriteLine("ex message: {0}", ex.Message);
    return new BitmapImage();
}
现在我可以得到异常,但仍然无法显示图像
https://p.qlogo.cn/gh/170883246/170883246_1/40

图像可以显示在WebBrowser中。我不知道为什么DotNet
BitmapImage
不能使用它。

你能发布cvtImage转换器吗?@richej我已经发布了转换器项170883246是问题所在。。不知道有什么问题。@richej不,我可以得到
https://p.qlogo.cn/gh/170883246/170883246_1/40
image是的,我知道,但它还是导致了问题。当你移除它时,它会工作。我首先加载一个图像的所有字节,然后将字节[]转换为位图。最后一步对于这个特定的图像是失败的。你能发布cvtImage转换器吗?@richej我已经发布了转换器项170883246是问题所在。。不知道有什么问题。@richej不,我可以得到
https://p.qlogo.cn/gh/170883246/170883246_1/40
image是的,我知道,但它还是导致了问题。当你移除它时,它会工作。我首先加载一个图像的所有字节,然后将字节[]转换为位图。对于此特定映像,最后一步失败。。
try 
{
    var c = new WebClient();
    var bytes = c.DownloadData(url);
    var ms = new MemoryStream(bytes);

    var bi = new BitmapImage();
    bi.BeginInit();
    bi.StreamSource = ms;
    bi.EndInit();

    return bi;  
}
catch (Exception ex)
{
    Console.WriteLine("ex message: {0}", ex.Message);
    return new BitmapImage();
}