window photo loading listbox images code

I am developing windows phone app where I am using listbox .In listbox I am binding some images.Here is my code..........

in code behind.....

    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; }
    }

in Xaml...

    <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>

1.All is working perfectly.But the problem is when the page is loading ,it is becoming so slow loading.Even it is taking so much time to load.I need a faster way to load these images,so that images can be loaded in listbox in seconds.Actually is there any method of binidng listbox images so that it will be loaded fastly.Plsss... help-.....

2.also when refreshing or loading page second time or clicling back button, in getimages() method in picture.getimage() it is showing out of memory exception(I have written in code).
Try using the VirtualizingStackPanel. Set the VirtualizationMode to "recycling". You can also call IsVirtualizing and make sure it is set to true. The ListBox should inherit from VirtualizingStackPanel so you should get this functionality for free. I'm concerned that your bottleneck is due to your call to picture.GetImage() the entire image is loaded into memory at this time. It is not clear what the object lifecycle is for the picture image if you call this for every picture. This certainly could cause out of memory errors if the underlying file stream is not released once the picture object goes out of scope. A better approach might be to call Picture.GetThumbnail and only load the high resolution image when selected.

  • Back aarticle:
  • Next aarticle: No
  • Copyright © 2007-2012 www.chuibin.com Chuibin Copyright