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