Lawrence
Lawrence

Reputation: 62

.net Compact Framework 2.0 - OutOfMemory Exception When downloading an image

Im creating software for a Symbol MC75A using c# .net CF 2.0. We scan a barcode and it returns stock information but i am trying to add a feature that gets an image from a url. Each scan refreshes the screen with new data from database and also gets the image from the new url. It scans a few barcodes and returns maybe 4/5 images without issue then all of a sudden a OutOfMemoryException occurs. The code im using to Get Image is:

        public Bitmap GetImage(string URL)
    {
        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
        myRequest.Method = "GET";

        myRequest.AllowWriteStreamBuffering = false;



        HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(myResponse.GetResponseStream());
            myResponse.Close();


            return bmp;

    }

Called by:

                        pbImage.Image = GetImage(ProductImage);
                    pbImage.SizeMode = PictureBoxSizeMode.StretchImage;

I have tried to dispose of image before each new GET with pbImage.Image.Dispose() but still getting the same exception. The image sizes are between 100KB and 550KB. Whether it makes a difference the images sizes are always over 1000px each side.

Am i missing the correct way of disposing before re-getting or is it somehow caching all these images which then creates an outofMemory exception?

Upvotes: 0

Views: 391

Answers (2)

Lawrence
Lawrence

Reputation: 62

Just incase someone has access to the image location. A better work around for me was to creat a webpage named resize.aspx and have the picture box populate from the page with the image filename as a parameter. Now that page (being on the server in the same location as the images) was able to resize the image and return a small 300x300px image. Seemed to be the best solution i could think of. This can also work for images at any url if you pass the full url of the image into another page and that page returns the required thumbnail.

Upvotes: 0

Lawrence
Lawrence

Reputation: 62

I have found my solution herer:

OutOfMemoryException loading big image to Bitmap object with the Compact Framework

It seems it is the system.drawing when decompressing the 2000px image it is creating an uncompressed image giving a larger size on the memory hense the exception. The solution shows a way to obtain a thumbnail of the image rather than the whole image.

Thanks for your help again.

Upvotes: 2

Related Questions