0

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
amal50
  • 981
  • 2
  • 21
  • 35
  • 1
    [use a finally at the end of your try catch block](http://stackoverflow.com/questions/10984336/net-using-using-blocks-vs-calling-dispose) or try a [using statement](http://stackoverflow.com/questions/2808753/right-way-to-dispose-image-bitmap-and-picturebox). I'm curious, what articles did you read on this? – lloyd Jun 06 '15 at 03:32
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 06 '15 at 03:38
  • so you think that I have to put it inside using? like this? .... using (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(); } – amal50 Jun 06 '15 at 03:45
  • this was example of bad solutions for the similar problem ........... http://stackoverflow.com/questions/15571022/how-to-find-reason-for-generic-gdi-error-when-saving-an-image – amal50 Jun 06 '15 at 03:48
  • @lloyd Thx, I did what you told me to do, but still I have the same error – amal50 Jun 06 '15 at 05:00
  • Using will call dispose. Please add code in the question. Very hard to read in comment – lloyd Jun 06 '15 at 07:51
  • Try [StreamWriter](http://stackoverflow.com/questions/16514530/c-sharp-using-statement-and-streamwriter) so you can write to the file. – lloyd Jun 06 '15 at 08:08
  • @lloyd ,I changed this (Rotate180FlipXY) to (Rotate180FlipNone) and after that the problem has been solved, it is really strange, I still don't know why Rotate180FlipXY only on 180 degree causes this problem, if I change the degree to 270 it will work fine, but Rotate180FlipXY causes this strange error, thanks anyway for effort – amal50 Jun 09 '15 at 03:35
  • no problem. maybe post the answer to this. – lloyd Jun 09 '15 at 04:05

1 Answers1

0

I changed this (Rotate180FlipXY) to (Rotate180FlipNone) and after that the problem has been solved

amal50
  • 981
  • 2
  • 21
  • 35