0

我有一个列表框,我正在从媒体库加载一些图像。但是当我第二次加载它或单击返回按钮时出现异常。这是我的代码............

 public MainPage()
{
InitializeComponent();
getImages();

}
public void getImages()
{
try
{
    MediaLibrary mediaLibrary = new MediaLibrary();
    var pictures = mediaLibrary.Pictures;
    foreach (var picture in pictures)
    {
        BitmapImage image = new BitmapImage();
        image.SetSource(picture.GetImage());//out of memory exception
        img mediaImage = new img();
        mediaImage.Imgs = image;
        imageList.Items.Add(mediaImage);
    }
}
catch (Exception ex)
{

}

}
public class img
{
public img()
{ }
public BitmapImage Imgs { get; set; }
}

在 Xml...

<ScrollViewer Name="sc" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" Margin="0,422,0,43">
        <ListBox Name="imageList" SelectionChanged="imageList_SelectionChanged_1" Height="126">
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate >
                    <StackPanel Orientation="Horizontal"></StackPanel>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate >
                    <StackPanel Orientation="Horizontal">
                    <Image Margin="10" Name="image1" Source="{Binding Imgs}" Height="150" Width="150"></Image>
                        </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>

在 picture.getimage() 中的 getimages() 方法中,它显示内存不足异常(我已经用代码编写过)。我尝试了 imageList.items.Clear()。但它不起作用。

所以请帮助......

4

0 回答 0