I am continually calling a method to make a webrequest to an IP camera for a JPEG image. I get the image and it is used by the main program. I do these calls continually as fast as possible. Eventually, the webrequest fails with the exception of "out of memory". Where am I not cleaning up I listed example code below. (Also, because of application restrictions, I am making a separate call for each image rather than reading streaming video)
using
System;using System.Drawing;
using
System.Drawing.Imaging;using
System.Collections;using
System.ComponentModel;using
System.Windows.Forms;using
System.Data;using
System.Runtime.InteropServices;using
System.Text;using
System.IO;using
System.Net; private Bitmap mNewBitmap; private Image mNewImage; Bitmap bmp; public bool GrabImageData(int buf){
try{
WebRequest WebReq = WebRequest.Create(mURL);
WebResponse WebRes = WebReq.GetResponse();
Stream ResponseStream = WebRes.GetResponseStream();mNewImage = Image.FromStream(ResponseStream);
ResponseStream.Close();
WebRes.Close();
mNewBitmap = (Bitmap) mNewImage;
return true;}
catch (System.Exception myException){
.....
return false;}
}

Why do my continual webrequests for images from an IP camera eventually end in "out of memory" exception?
MattinSpace
Couple of possibile reasons:
You're not disposing the images. I don't see any Dispose in this code but maybe you have it in another part of the application, do you
If an exception is thrown in this code then ResponseStream and WebRes may not be disposed. You should use "using":
...
using (WebResponse webRes = webReq.GetResponse())
using (Stream responseStream = webRes.GetResponseStream())
{
mNewImage = Image.FromStream(responseStream);
}
mNewBitmap = (Bitmap)mNewImage;
return true;