Click here to Skip to main content
15,921,113 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi, i am saving image in folder. i gave correct location and gave write permisstion for that particular folder, but still i am getting error " A generic error occured in GDI +".

here my code snippets:

conti,...

C#
MemoryStream oStream = new MemoryStream();
bitmap.Save(oStream, System.Drawing.Imaging.ImageFormat.Png);

string location = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;  // till now no erro. 
bitmap.Save(location + "Image\\temp\\" + Session.SessionID + "\\" + PhotoID.Trim() + ".Jpeg", ImageFormat.Jpeg); // here i am getting that error.
Response.BinaryWrite(oStream.ToArray());
bitmap.Dispose();


how to solve this one. Dispose();
Posted
Updated 24-Dec-13 2:39am
v2
Comments
Richard MacCutchan 24-Dec-13 12:19pm    
Check that the final path and filename you are creating are valid.

I think it is related to how bitmap was created.

Basically:
If bitmap was created from memory stream, you must keep the stream open for the lifetime of the Bitmap. Otherwise GDI+ Exception is thrown ("A generic error occurred in GDI+".) when you try to save bitmap.

You don't show how bitmap is created but from behavior you describing I'm almost certain this is it.



Also check this:

http://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream[^]

and this:

http://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed[^]
 
Share this answer
 
When Bitmap object constructed from a file, the file remains locked for the lifetime of the object. So cannot modify image and save it back to the same file where it constructed.

Try like below

C#
using (MemoryStream oStream = new MemoryStream())
{
    using (FileStream fs = new FileStream(location, FileMode.Create, FileAccess.ReadWrite))
    {
        bitmap.Save(oStream , ImageFormat.Png);
        byte[] bytes = oStream.ToArray();
        fs.Write(bytes, 0, bytes.Length);
    }
}


Hope this helps you...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900