I have a code that rotate image in my asp.net C# application this is my code:
protected void ImgBtn180_Click(object sender, ImageClickEventArgs e)
{
try
{
string vImageName = LblFarmId.Text;
string vPath = "~/attachments/survey/" + vImageName + ".jpg";
Image1.ImageUrl = vPath;
//get the path to the image
string path = Server.MapPath(vPath);
//create an image object from the image in that path
System.Drawing.Image img = System.Drawing.Image.FromFile(path);
//rotate the image
img.RotateFlip(RotateFlipType.Rotate180FlipXY);
//save the image out to the file
img.Save(path);
//release image file
img.Dispose();
Random rnd = new Random();
Image1.ImageUrl = vPath + "?" + rnd;
}
catch (Exception ee)
{
LblCatchError.Text = ee.ToString();
}
}
when I run it on the server, I'm getting sometimes the following error
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format) at System.Drawing.Image.Save(String filename)
and sometimes the following error
System.OutOfMemoryException: Out of memory. at System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) at System.Drawing.Image.FromFile(String filename)
I read all articles and solutions about this error, and nothing worked. some said, "make sure that the desired folder has Read/Write permissions." it has permissions..
some said , "you must dispose image object to release the memory on the server." , I already have the code img.Dispose(); to release image file,
What might be the problem in the code? any advice?