0

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?

Lawrence
  • 62
  • 1
  • 9
  • the images are going into temporary IE files. You may want to delete you browsing history. Sometime using a sniffer like fiddler or wireshark can help isolate downloading issues. – jdweng Nov 08 '15 at 14:32
  • Is it always breaking on the same image? System.Drawing is famous for throwing OutOfMem for lots of other reasons. Those can be handled with a try/catch. – H H Nov 08 '15 at 14:35
  • Your `myRespones.Close()` should be in a finally or in a using block. Are you sure there are no other exceptions? – H H Nov 08 '15 at 14:37
  • @HenkHolterman - Ive put it in a try/catch and finally block as suggested but still only recieving a OutOfMemoryException. Ive narrowed it down to image resolution. The filesize is 576KB on this one particular image that keeps giving the error. This image is 2426 x 2480 px. Would the image size create a larger stream that the actual file size? Or maybe the System.Drawing cannot find the resources to deal with the size. Apart from changing all images over a certain res is there a way to download a compressed version? – Lawrence Nov 08 '15 at 17:06
  • Why do you wonder that the system can not load that large images into memory? 2400x2400 pixel means about 17MB data (RGB 24Bit) to fit with your process in a 32MB slot. And whyt happens to the image? It may be displayed on a 480x640 pixel screen, what a waste of resources! You should display and load thumbnails only. There will be no more details if display such large image on a small screen. – josef Nov 09 '15 at 05:55

2 Answers2

2

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.

Community
  • 1
  • 1
Lawrence
  • 62
  • 1
  • 9
0

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.

Lawrence
  • 62
  • 1
  • 9