i can't find error in code every check gave me error " file open in program" think there stream haven't dispose
public static void checkresolution(string imagepath) { var image = loadsigleimagefromfile(imagepath); var basearea = 2600 * 1000;//dimenzioni in risoluzione filestream stream = new filestream(image.fileinfo.fullname, filemode.open, fileaccess.readwrite); try { image img = image.fromstream(stream); var imagearea = img.height * img.width; if (imagearea >= basearea) { var scalefactor = (imagearea / basearea); var newverticalres = (int)(img.height / scalefactor); var newhorizontalres = (int)(img.width / scalefactor); var newimage = resizeimage(img, new size(newhorizontalres, newverticalres)); if (file.exists(imagepath)) file.delete(imagepath); newimage.save(imagepath, imageformat.jpeg); } } catch (exception ex) { logger.error("errore scala foto : " + ex.message); //if (boolean.parse(configurationmanager.appsettings["stoponexception"])) throw new exception("checkresolution errore scala foto : " + ex.message); } { stream.dispose(); } }
here loadsingle... function
public static imagefromfile loadsigleimagefromfile(string file) { var ris = new imagefromfile(); fileinfo fileinfo = new fileinfo(file); if (fileinfo.name != "thumbs.db") ris = (new imagefromfile() { fileinfo = fileinfo }); return ris; }
update resizeimage function
private static image resizeimage(image imgtoresize, size size) { int sourcewidth = (int)imgtoresize.width; int sourceheight = (int)imgtoresize.height; float npercent = 0; float npercentw = 0; float npercenth = 0; npercentw = ((float)size.width / (float)sourcewidth); npercenth = ((float)size.height / (float)sourceheight); if (npercenth < npercentw) npercent = npercenth; else npercent = npercentw; int destwidth = (int)(sourcewidth * npercent); int destheight = (int)(sourceheight * npercent); bitmap b = new bitmap(destwidth, destheight); b.setresolution(imgtoresize.horizontalresolution, imgtoresize.verticalresolution); graphics g = graphics.fromimage((system.drawing.image)b); g.interpolationmode = interpolationmode.highqualitybicubic; g.drawimage(imgtoresize, 0, 0, destwidth, destheight); g.dispose(); return (image)b; }
it evident in code block of lines
if (file.exists(imagepath)) file.delete(imagepath);
tries delete same file opened stream above. should try pospone deletion of file (and following save) after closing of stream opened before.
i suggest these changes
public static void checkresolution(string imagepath) { // flag becomes true if resize operation completes bool resizecompleted = false; image newimage = null; // if file doesn't exist code below returns null. var image = loadsigleimagefromfile(imagepath); if(image == null) return; var basearea = 2600 * 1000;//dimenzioni in risoluzione using(filestream stream = new filestream(image.fileinfo.fullname, filemode.open, fileaccess.readwrite)) { try { image img = image.fromstream(stream); var imagearea = img.height * img.width; if (imagearea >= basearea) { ... resize ops .... // if (file.exists(imagepath)) //file.delete(imagepath); // newimage.save(imagepath, imageformat.jpeg); // set flag true if resize completes resizecompleted = true; } } catch (exception ex) { logger.error("errore scala foto : " + ex.message); throw new exception("checkresolution errore scala foto : " + ex.message); } } // can delete , save.... if(resizecompleted) { // no need check existance. file.delete doesn't throw if // file doesn't exist file.delete(imagepath); newimage.save(imagepath, imageformat.jpeg); } } public static imagefromfile loadsigleimagefromfile(string file) { // check if file exists otherwise return null.... var ris = null; if(file.exists(file)) { fileinfo fileinfo = new fileinfo(file); if (fileinfo.name != "thumbs.db") ris = (new imagefromfile() { fileinfo = fileinfo }); } return ris; }
moving delete , save operations after closing brace of using block aure file no more locked own program , proceed delete , save actions
notice should check existance of input file before entering code, otherwise exception here wait you.
Comments
Post a Comment