0

I have an error " A Generic error occured in GDI+" when i try to serialize a new bitmap image in my xml file. The error only happen when there is an existing data in the file, if i'ts empty the serialization works.

Here is the code:

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
[XmlElement("LargeIcon")]
public byte[] LargeIconSerialized
{
    get
    { // serialize
        if (_Photo == null) return null;
        using (MemoryStream ms = new MemoryStream())
        {
            _Photo.Save(ms, ImageFormat.Bmp);
            return ms.ToArray();
        }
    }
    set
    { // deserialize
        if (value == null)
        {
            _Photo = null;
        }
        else {
            using (MemoryStream ms = new MemoryStream(value))
            {
                _Photo = new Bitmap(ms);
            }
        }
    }
}

Where I serialize:

public void EnregistrerNewUtil(string folderPath, string fileName)
{
    Stream fStream;

    try { // si ouvert ferme le fichier
        fStream = new FileStream(folderPath + fileName, FileMode.Open, FileAccess.Write, FileShare.None);
        fStream.Close();
    }
    catch{
        // déjà fermé
    }

    using (fStream = new FileStream(folderPath + fileName, FileMode.Truncate, FileAccess.Write, FileShare.None))//vider avant de réécrire la liste
    {
        XmlSerializer xmlFormat = new XmlSerializer(typeof(UtilisateurList));
        xmlFormat.Serialize(fStream, list);
    }
}

edit

I should add that i serialize a list, here is the code :

    [XmlRoot("Utilisateur_List")]
    public class UtilisateurList
    {
        public UtilisateurList() { Items = new List<Utilisateur>(); }
        [XmlElement("Utilisateur")]
        public List<Utilisateur> Items { get; set; }
    }

Thanks

Remi Hirtz
  • 134
  • 3
  • 16
  • Is the file closed() before you are writing? – jdweng Apr 20 '16 at 20:10
  • No but how can I close it because the serialization is in my class `User` and the call `xmlFormat.Serialize(fStream, list);` is in a form `UserManagement` – Remi Hirtz Apr 20 '16 at 20:15
  • See [A generic error occurred in GDI+, JPEG Image to MemoryStream](https://stackoverflow.com/questions/1053052/a-generic-error-occurred-in-gdi-jpeg-image-to-memorystream/1053123#1053123): *It appears that the memory stream that the object was created on has to be open at the time the object is saved.*. See also [Image.Save(..) throws a GDI+ exception because the memory stream is closed](https://stackoverflow.com/questions/336387) – dbc Apr 20 '16 at 20:21
  • It's already the case `using (MemoryStream ms = new MemoryStream()) { _Photo.Save(ms, ImageFormat.Bmp); return ms.ToArray(); }` – Remi Hirtz Apr 20 '16 at 20:27
  • Then try removing the `using` from `using (MemoryStream ms = new MemoryStream(value))` and just keep the stream open as is suggested [here](https://stackoverflow.com/questions/336387/image-save-throws-a-gdi-exception-because-the-memory-stream-is-closed). – dbc Apr 20 '16 at 20:53
  • Oh shit I red the article you put in link, but I removed the using in the get :p Thanks a lot – Remi Hirtz Apr 20 '16 at 20:57

0 Answers0