Hi,
I'm having a problem with releasing a file lock after a save. My code takes an image file uploaded via a form, and saves it to disk. That's fine, but I'm trying to add a new function which allows the user to choose to automatically create a thumbnail. At this stage, it throws a "System.OutOfMe moryException: Out of memory." Even if the files are small, I still get this, and I find that if I open up Windows Explorer and try to delete the large image, it's locked by the aspnet_wp process, so I'm guessing it's this lock that's causing the exception (which I know doesn't always mean it's actually out of memory!).
I have tried everything I can think of to release the lock (see the gubbins below!) but it's still not working. Any ideas?
I'm having a problem with releasing a file lock after a save. My code takes an image file uploaded via a form, and saves it to disk. That's fine, but I'm trying to add a new function which allows the user to choose to automatically create a thumbnail. At this stage, it throws a "System.OutOfMe moryException: Out of memory." Even if the files are small, I still get this, and I find that if I open up Windows Explorer and try to delete the large image, it's locked by the aspnet_wp process, so I'm guessing it's this lock that's causing the exception (which I know doesn't always mean it's actually out of memory!).
I have tried everything I can think of to release the lock (see the gubbins below!) but it's still not working. Any ideas?
Code:
public static Upload uploadFile(HtmlInputFile inputFile, string sSavePath, bool createThumb)
{
System.Web.HttpServerUtility Server=HttpContext.Current.Server;
// If file field isn't empty
if (inputFile.PostedFile.ContentLength > 0)
{
// Check file size (mustn't be 0)
HttpPostedFile myFile = inputFile.PostedFile;
int iFileLen = myFile.ContentLength;
//<snip> do some checks to see if it's accepted file type etc </snip>
// Read file into a data stream
byte[] myData = new Byte[iFileLen];
myFile.InputStream.Read(myData,0,iFileLen);
// Save the stream to disk
System.IO.FileStream newFile = new System.IO.FileStream(savepath, System.IO.FileMode.Create);
try
{
newFile.Write(myData,0, myData.Length);
}
catch{}
//anything i can think of to clear the file lock
inputFile.Dispose();
newFile.Flush();
newFile.Close();
newFile = null;
myFile = null;
GC.Collect();
if(createThumb)
{
string[] pth = savepath.Split('.');
string thumbpath = "";
for(int i=0;i<pth.Length-1;i++)
thumbpath += pth[i];
thumbpath += "_thumb";
thumbpath += pth[pth.Length-1];
createThumbnail(savepath,thumbpath,150,150);
}
}
}
public static void createThumbnail(string FullImagePath, string thumbnailPath, int maxWidth, int maxHeight)
{
//read the full size image -- ***This is where the exception is thrown***
System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(FullImagePath);
// check the sizes to see if we need to resize
bool resize = false;
int thumbWidth = fullSizeImg.Width;
int thumbHeight = fullSizeImg.Height;
//check to see if the width is too large, set new sizes if required
if(thumbWidth > maxWidth)
{
resize = true;
thumbWidth = maxWidth;
thumbHeight = maxWidth/fullSizeImg.Width * fullSizeImg.Height;
}
// then we have to check to see if the height is within the constriants and try again
if(thumbHeight > maxHeight)
{
resize = true;
thumbHeight = maxHeight;
thumbWidth = maxHeight/fullSizeImg.Height * fullSizeImg.Width;
}
if(resize)
{
System.Drawing.Image thumbNailImg = fullSizeImg.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
thumbNailImg.Save(thumbnailPath,ImageFormat.Jpeg);
//Clean up / Dispose...
thumbNailImg.Dispose();
}
}
Comment