c# - A Generic error occurred in GDI+ in Bitmap.Save method -


i working on upload , save thumbnail copy of image in thumbnail folder.

i using following link:

http://weblogs.asp.net/markmcdonnell/archive/2008/03/09/resize-image-before-uploading-to-server.aspx

but

newbmp.save(directory + "tn_" + filename);    

is causing exception "a generic error occurred in gdi+."

i have tried give permission on folder, tried use new separate bmp object when saving.

edit:

    protected void resizeandsave(propbannerimage objpropbannerimage)     {         // create bitmap of content of fileupload control in memory         bitmap originalbmp = new bitmap(fuimage.filecontent);          // calculate new image dimensions         int origwidth = originalbmp.width;         int origheight = originalbmp.height;         int sngratio = origwidth / origheight;         int thumbwidth = 100;         int thumbheight = thumbwidth / sngratio;          int bannerwidth = 100;         int bannerheight = bannerwidth / sngratio;          // create new bitmap hold previous resized bitmap         bitmap thumbbmp = new bitmap(originalbmp, thumbwidth, thumbheight);         bitmap bannerbmp = new bitmap(originalbmp, bannerwidth, bannerheight);          // create graphic based on new bitmap         graphics ographics = graphics.fromimage(thumbbmp);         // set properties new graphic file         ographics.smoothingmode = smoothingmode.antialias; ographics.interpolationmode = interpolationmode.highqualitybicubic;          // draw new graphic based on resized bitmap         ographics.drawimage(originalbmp, 0, 0, thumbwidth, thumbheight);          bitmap newbitmap = new bitmap(thumbbmp);         thumbbmp.dispose();         thumbbmp = null;          // save new graphic file server         newbitmap.save("~/image/thumbs/" + "t" + objpropbannerimage.imageid, imageformat.jpeg);          ographics = graphics.fromimage(bannerbmp);         // set properties new graphic file         ographics.smoothingmode = smoothingmode.antialias; ographics.interpolationmode = interpolationmode.highqualitybicubic;          // draw new graphic based on resized bitmap         ographics.drawimage(originalbmp, 0, 0, bannerwidth, bannerheight);         // save new graphic file server         bannerbmp.save("~/image/" + objpropbannerimage.imageid + ".jpg");           // once finished bitmap objects, deallocate them.         originalbmp.dispose();          bannerbmp.dispose();         ographics.dispose();     } 

when either bitmap object or image object constructed file, file remains locked lifetime of object. result, cannot change image , save same file originated. http://support.microsoft.com/?id=814675

a generic error occurred in gdi+, jpeg image memorystream

image.save(..) throws gdi+ exception because memory stream closed

http://alperguc.blogspot.in/2008/11/c-generic-error-occurred-in-gdi.html

edit:
writing memory...

save 'intermediary' memory stream, should work

e.g. try 1 - replace

    bitmap newbitmap = new bitmap(thumbbmp);     thumbbmp.dispose();     thumbbmp = null;     newbitmap.save("~/image/thumbs/" + "t" + objpropbannerimage.imageid, imageformat.jpeg); 

with like:

string outputfilename = "..."; using (memorystream memory = new memorystream()) {     using (filestream fs = new filestream(outputfilename, filemode.create, fileaccess.readwrite))     {         thumbbmp.save(memory, imageformat.jpeg);         byte[] bytes = memory.toarray();         fs.write(bytes, 0, bytes.length);     } } 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -