File in Use?

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();
 
P

Peter Rilling

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."
 
H

Harry Whitehouse

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top