c# - Cannot save file after opening it -
i can save file infinitely. can open file infinitely. , can save open file.
however cannot save file after opening it. following error:
a first chance exception of type 'system.runtime.interopservices.externalexception' occurred in system.drawing.dll
additional information: generic error occurred in gdi+.
i tried disposing , temp bitmaps, did not seem work me. location file being opened , saved same place, perhaps maybe problem overwriting file? program breaks @ temp.save
private void opentoolstripmenuitem_click(object sender, eventargs e) { //graphics.fromimage(bmap).dispose(); using (graphics g = graphics.fromimage(bmap)) { bmap = new bitmap(@"c:\users\nick\final.bmp"); g.drawimage(bmap, panel1.width, panel1.height); } panel1.invalidate(); } private void savetoolstripmenuitem_click(object sender, eventargs e) { graphics g = graphics.fromimage(bmap); temp =new bitmap(bmap); //graphics.fromimage(bmap).dispose(); try { g.dispose(); temp.save(@"c:\users\nick\final.bmp", imageformat.bmp); } catch (unauthorizedaccessexception ex) { console.writeline(ex.message); ; } catch (system.runtime.interopservices.externalexception ex) { console.writeline(ex.message); } }
you can't overwrite file, because when use new bitmap(path)
file loaded in memory , keep open until you're done it, can't overwrite file if it's open.
edit
here how load image memory stream:
// make sure don't dispose of until you're done var memorystream = new memorystream(file.readallbytes(path)); image img = image.fromstream(memorystream);
Comments
Post a Comment