2

I am having trouble saving a user uploaded image to my "logos" file when the user presses "Save Team" on my form.

For you to get a taster of what the application does I have provided a screenshot here, upon pressing add team all the text box data is written to a text file and I want the image to automatically save into a predefined folder "logos", however I get the GDI++ error whenever I press save.

After reading through a few old threads on the web, I found that it could be caused by a few things such as file permissions, file size, the files name or even an open stream.

Below is the code I am currently using to save out my file which prompts the error:

     private void btnAddTeam_Click(object sender, EventArgs e)
    {
        //VALIDATION FOR TEXT FILE AND OTHER STUFF HERE...

        //SAVE THE IMAGE FILE
        string filePath = picTeamLogo.ImageLocation;
        Image tempImage = Image.FromFile(filePath);

        tempImage.Save(@"../logos/");
    }

If you are viewing the screenshot, please do not get confused by "arsenal.png" I am aware that that is not the full file path, its conversion is handled in another method, as only the filename is required to be written to the text file.

If anybody has any ideas on where I am going wrong then please point me in the right direction, vague errors such as the GDI one I just received are such a headache!


Alex.

Halfpint
  • 3,967
  • 9
  • 50
  • 92
  • 1
    can you provide the exception with a stacktrace? – DarkSquirrel42 Apr 08 '13 at 18:51
  • Looks like you need to specify a filename rather than just a directory. @"../logos/test.bmp", for example. – Gray Apr 08 '13 at 18:51
  • possible duplicate of [A Generic error occured in GDI+ in Bitmap.Save method](http://stackoverflow.com/questions/15862810/a-generic-error-occured-in-gdi-in-bitmap-save-method) – Kirk Woll Apr 08 '13 at 18:51
  • Thanks Kirk I will check out that post, I did try to search other threads before posting my own, however I have not yet seen that one! – Halfpint Apr 08 '13 at 18:58

3 Answers3

2

Try this method

 public void storeFile(FileUpload File, string dirName)
    {
        if (!Directory.Exists(MapPath("~/uploads/" + dirName)))
        {
            Directory.CreateDirectory(MapPath("~/uploads/" + dirName));
        }
        string saveLocation = MapPath("~/uploads/" + dirName + "/" +        Path.GetFileName(File.FileName));
        File.SaveAs(saveLocation);


    }
foo-baar
  • 1,076
  • 3
  • 17
  • 42
1

See this earlier post of mine for similar issue...

A Generic error occurred in GDI+ in Bitmap.Save method

The stream remains locked - you can use memory stream as a temp location then save.

When either a Bitmap object or an Image object is constructed from a file, the file remains locked for the lifetime of the object. As a result, you cannot change an image and save it back to the same file where it originated. http://support.microsoft.com/?id=814675

string outputFileName = "...";
using (MemoryStream memory = new MemoryStream())
{
    using (FileStream fs = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite))
    {
        tempImage.Save(memory, ImageFormat.Jpeg);
        // memory.ToStream(fs) // I think the same
        byte[] bytes = memory.ToArray();
        fs.Write(bytes, 0, bytes.Length);
    }
}

Or alternative is loading image via MemoryStream into the image - so the stream doesn't get locked in the first place...

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
1

that's because you have not specified the file name for save function !

take a look at this :

string filePath = picTeamLogo.ImageLocation;
FileInfo fi = new FileInfo(filePath);
Image tempImage = Image.FromFile(fi.FullName);
tempImage.Save(@"../logo/" + fi.Name);

now it works properly

Mehran
  • 1,409
  • 2
  • 17
  • 27