views:

76

answers:

4

Hi! I'm making an application which uses MANY images. The application gets the images from a server, and downloads them one at a time. After many images the creation of a bitmap returns an exception, but i don't know how to solve this. Here is my function for downloading the images:

 public static Bitmap getImageFromWholeURL(String sURL)
    {

        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(sURL);
        myRequest.Method = "GET";


        // If it does not exist anything on the url, then return null
        try
        {
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
            myResponse.Close();
            return bmp;
        }
        catch (Exception e)
        {
            return null;
        }

      }

Can anyone help me out here? Thanks in advance!

A: 

"Many images" is of course closely associated with running out of memory. Bitmaps can get large, they'll eat up a lot of unmanaged virtual memory. You'll have to make your program smarter and store less bitmaps in memory. Or save them temporarily to a file. Or re-download them if necessary. And properly clean up their resources with the Dispose() method, especially important for the Bitmap class.

Hans Passant
every image i download, i use in a picturebox. So that might be what is causing it.
Yes, that's going to come to a screeching end sooner or later. Only use a fixed number of PBs, write a scrolling algorithm that re-uses the existing PBs for new pictures. Don't forget to Dispose() the old ones.
Hans Passant
yes, i have made a scrolling algorithm, but it did not dispose the any PB's, but now it is implemented and working :)
But it will probably do better by reusing the PB's instead of deleting the whole PB and insert new if scrolling to that area.
A: 

What do you do with the System.Drawing.Bitmap objects? Do you keep all of them in memory? Then it is inevitable that you'll get an out of memory exception at some point.

Based on your needs you should discard the images at some point. If you do need them, store them in a file in flash. Also, try using files of smaller sizes.

kgiannakakis
every image i download, i use in a picturebox. So that might be what is causing it.
i am downloading small .png files at a size of max 250kb.
The size of the png file doesn't matter. It is the dimensions of the image that count. When converted to a Bitmap, the memory needed will be width*height*[number of bits per pixel]. Number of bits depend on PixelFormat (it could be 16, 24 or 32 bit). I don't know which is the default. You could try using a different constructor to adjust this.
kgiannakakis
+1  A: 

Stream that response to disk rather than keep it in memory. Then keep around the information about the image you've saved to a temporary place instead of the image itself.

Will
A: 

If you're showing these all in a Picturebox (and based on your comments I think you are) then you should Dispose of the old images (this blog entry helps explain it):

if(myPictureBox.Image != null)
{
    myPictureBox.Image.Dispose();
}
myPictureBox.Image = getImageFromWholeURL(url);

As a side note on style, method names are supposed to be PascalCase, not camelCase and I'd lose the hungarian notion on the parameter.

ctacke
Ah, sorry a bit misunderstanding what i commented about picturebox. Every image gets its own picturebox. But i think i have found a solution i can live with.I have an algorithm that checks the nearest neighbour, and checks if it has an image. If neighbour does not contain an image -> download image.What i also need to do now is to have an algorithm that removes images that is not in the view.This way i will not keep that many images in memory.