Question

For reading a picture from a folder with my Silverlight application, I Set the source of a Bitmap image with the stream of the file. See the code below:

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Images", String.Format("{0}.jpg", _imageName));
if (File.Exists(path))
{
    Image = new BitmapImage();
    Image.SetSource(new MemoryStream(File.ReadAllBytes(path)));
}

The problem is that the image take a lot of time to show up and when I load a lot of pictures ( >400), I may get a insufficient memory error. I never had this error when loading a picture by the URI and I was wondering if it was possible to load it by the URI from a path. The code I tried:

string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Images", String.Format("{0}.jpg", _imageName));
if (File.Exists(path))
{
    Image = new BitmapImage()
                {
                    UriSource = new Uri(path),
                    CreateOptions = BitmapCreateOptions.DelayCreation
                };
}

Do you have any hints to provide ?

Thank you!

Philippe

Was it helpful?

Solution 3

I realized that loading even when I loaded the pictures by the URI, it wasn't loading every pictures. The software stopped loading pictures when it was taking 1.6gig of ram (out of 6gig). The difference with loading the picture by the stream is that it seems there is no protection against insufficient memory.

As I don't display all the images ( I realized I have over 8000 pictures when they are loaded correctly) I only load in memory the image I need to display.

So every time I want a picture, I load it from the Hard Drive. When the pictures is no more displayed, the garbage collection get rid of them. This way, the application's memory is always stable at around 300 meg.

OTHER TIPS

My guess would be you are loading photos from the local file system that have a much higher resolution than the images you are loading via a URL.

You wanna load 400 of 'em, yes you are going to run out of memory.

There is not much you can do about the loading time. You might be able to reduce the pressure on memory by using a WriteableBitmap to scale the actual bitmap down.

Load image into your property resize it and set that image as preview, then dispose the image from the folder and move to the next one.

So in short, you should create thumbnails of the images in that folder and show those in your app.

What you now do is keep all the images in your memory, all the big images that is, thats why you get out of memory exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top