File in Use?

  • Thread starter Thread starter Harry Whitehouse
  • Start date Start date
H

Harry Whitehouse

Hello!

I have some ASPX code which creates a JPEG file in response to a button
press. The JPEG file is contructed in memory (see below), stored to a JPEG
file on the server, and then dished-out on the following browser window.

Everything works, but if I try to do the process again, I get a message
saying that the JPEG image file is in use. I can only free it by restarting
Web services. (I can't even delete the file using Windows Explorer!)
Clearly, I must not be releasing this resource properly.

Can anyone see what I'm missing?

TIA

Harry


// Create the bitmap object to draw our label
chartBmp = new Bitmap(width, height);

// Create the Graphics object.

chartGraphics = Graphics.FromImage(chartBmp);

// Fill the background of our choice.

chartGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0,
width,height);

// top horizontal

Point pt1 = new Point(0,0);

Point pt2 = new Point(width,0);

chartGraphics.DrawLine(Pens.Black,pt1,pt2);



String chartFile = Server.MapPath("images/LabelImage.jpg");

FileStream jpgFile = new FileStream(chartFile, FileMode.Create,
FileAccess.ReadWrite);



// Save the graphics as Jpeg file.

chartBmp.Save(jpgFile, ImageFormat.Jpeg);



Response.Clear();



chartBmp.Save(Response.OutputStream, ImageFormat.Jpeg);



Response.End();



jpgFile.Close();



chartGraphics.Dispose();

chartBmp.Dispose();
 
It may be because you are not closing your file. Below is something from
the HttpResponse.End method docs. In this case you may also end up with
memory leaks since your dispose methods are not being called.

"Sends all currently buffered output to the client, stops execution of the
page, and raises the Application_EndRequest event."
 
Peter -- That was the hint I needed.

I needed to close the JPG file BEFORE I sent the response back.

Thanks for your help!!

Best

Harry
 
Back
Top