This is an Asp.Net error occurs when you try to access a File object immediately after you use it. This error means the CLR otherwise the .NET Framework doesn’t release the resource yet after it is being used successfully. Or this error can even come when some problem is encountered in between processing that file. The way to handle this is simple.
The ‘using’ statement helps to overcome this problem. File and Font classes are managed objects that can access unmanaged resources such as Images, Fonts, or other files in the server. When we create instances for these classes, it is generally better to instantiate the object in the using statement and limit its scope to the using block. The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object and releases the resources used by the objects perfectly.
A general syntax is given below
using (System.Drawing.Image Img = System.Drawing.Image.FromFile(Server.MapPath("Images\\TestImage.jpg")))
{
// Do some operation here
}
|