c# - Can't delete or rename original file after resizing -


this function return resized , centered image, , execute calling url thumb.aspx?image=test.jpg&width=100&height=50 problem after execution, i can't rename or delete original file server.

  <%@ import namespace="system.drawing" %>     <script language="c#" runat="server">     void page_load(object sender, eventargs e) {       try {         response.cache.varybyparams["image;width;height;needtofill"] = true;         response.contenttype = "image/jpeg";         system.collections.hashtable imageoutputformatstable = new system.collections.hashtable();         imageoutputformatstable.add(system.drawing.imaging.imageformat.gif.guid, system.drawing.imaging.imageformat.gif);         imageoutputformatstable.add(system.drawing.imaging.imageformat.jpeg.guid, system.drawing.imaging.imageformat.jpeg);         imageoutputformatstable.add(system.drawing.imaging.imageformat.bmp.guid, system.drawing.imaging.imageformat.gif);         imageoutputformatstable.add(system.drawing.imaging.imageformat.tiff.guid, system.drawing.imaging.imageformat.jpeg);         imageoutputformatstable.add(system.drawing.imaging.imageformat.png.guid, system.drawing.imaging.imageformat.jpeg);         string imagelocation = server.mappath(request.querystring["image"]);         int width = convert.toint32(request.querystring["width"]);         int height = convert.toint32(request.querystring["height"]);         system.drawing.bitmap image = new system.drawing.bitmap(imagelocation);         int sourcewidth = image.width;         int sourceheight = image.height;         int sourcex = 0;         int sourcey = 0;         double destx = 0;         double desty = 0;         double nscale = 0;         double nscalew = 0;         double nscaleh = 0;         bool needtofill=true;         nscalew = ((double)width / (double)sourcewidth);         nscaleh = ((double)height / (double)sourceheight);          if (request.querystring["needtofill"] != null) {             needtofill = convert.toboolean(request.querystring["needtofill"]);         }          if (!needtofill) {             nscale = math.min(nscaleh, nscalew);         } else {             nscale = math.max(nscaleh, nscalew);             desty = (height - sourceheight * nscale) / 2;             destx = (width - sourcewidth * nscale) / 2;         }          if (nscale > 1) nscale = 1;          int destwidth = (int)math.round(sourcewidth * nscale);         int destheight = (int)math.round(sourceheight * nscale);          system.drawing.bitmap bmphoto = new system.drawing.bitmap(destwidth + (int)math.round(2 * destx), destheight + (int)math.round(2 * desty));         bmphoto.setresolution(72, 72);         system.drawing.imaging.imageformat outputformat = (system.drawing.imaging.imageformat)imageoutputformatstable[image.rawformat.guid];         applicationexception ex= new applicationexception(string.format("destwidth:{0}, destx:{1}, destheight:{2}, desxty:{3}, width:{4}, height:{5}", destwidth, destx, destheight, desty, width, height));         system.drawing.graphics grphoto = system.drawing.graphics.fromimage(bmphoto);         rectangle =  new system.drawing.rectangle((int)math.round(destx), (int)math.round(desty), destwidth, destheight);         rectangle = new system.drawing.rectangle(sourcex, sourcey, sourcewidth, sourceheight);         grphoto.drawimage(image, to, from, system.drawing.graphicsunit.pixel);          bmphoto.save(response.outputstream, outputformat);         bmphoto.dispose();         grphoto.dispose();       }       catch (exception ex){         system.io.streamwriter sw=null;         try{             sw=new system.io.streamwriter(server.mappath("error.txt"),true);             sw.writeline("error : " + ex.message + " processing " + request.querystring["image"]);         }             catch{}              finally{sw.close();}         response.redirect("thumberror.gif");       }     }     </script> 

image doesn't it's being disposed. i'd recommend using using statement make resource management more clear.

a using statement allows placing in scope, , when scope left, gets dispose called on it. example:

using(var image = new bitmap(imagelocation)) {     //use image here } //image disposed here 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -