Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2659 connectés 

  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  [C#] Serializer un objet 'Font'

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

[C#] Serializer un objet 'Font'

n°1286684
djmanu22
Posté le 18-01-2006 à 15:44:03  profilanswer
 

Hello,
je cherche à Sérialiser un objet 'Font' mais cela ne fonctionne pas car cette classe n'est pas Serializable.
Je suppose que je dois passer par un objet sérializable à moi avec les infos de cette police (Nom,Taille, Bold, etc) ca fonctionne mais je ne sais pas comment récreer un objet 'Font' à la désérialization car les propriétés de la classe Font sont readonly.
Avez-vous une idée ?
Merci d'avance

mood
Publicité
Posté le 18-01-2006 à 15:44:03  profilanswer
 

n°1287723
ixemul
Nan mais sans blague ! ⚡
Posté le 19-01-2006 à 17:19:06  profilanswer
 

j'ai deja été confronté à ce probleme  :)  !  
 
voici la solution mise en oeuvre pour palier à ça:
 
 

Code :
  1. public struct XmlFont
  2.  {
  3.   public string FontFamily;
  4.   public System.Drawing.GraphicsUnit GraphicsUnit;
  5.   public float Size;
  6.   public System.Drawing.FontStyle Style;
  7.   public XmlFont(System.Drawing.Font f)
  8.   {
  9.    FontFamily = f.FontFamily.Name;
  10.    GraphicsUnit = f.Unit;
  11.    Size = f.Size;
  12.    Style = f.Style;
  13.   }
  14.   public System.Drawing.Font ToFont()
  15.   {
  16.    return new System.Drawing.Font(FontFamily, Size, Style,
  17.     GraphicsUnit);
  18.   }
  19.  }


 
 
puis, pour l'utiliser :
 
Serialisation (puis passage en paramètre a une procédure stockée ici):

Code :
  1. System.IO.Stream toCopy = new System.IO.MemoryStream();
  2.     XmlFont FontToSerialize = new XmlFont(this.CustomFont);
  3.     XmlSerializer ttt = new XmlSerializer(typeof(XmlFont));
  4.     ttt.Serialize(toCopy, FontToSerialize);
  5.     toCopy.Flush();
  6.     toCopy.Position = 0;
  7.     System.IO.TextReader toRead =(System.IO.TextReader) new System.IO.StreamReader( toCopy);
  8.     command.Parameters.Add("@Font",SqlDbType.Text).Value =toRead.ReadToEnd();


 
 
puis la déserialisation (obtenue d'un Datarow) :
 

Code :
  1. System.IO.Stream toCopy = new System.IO.MemoryStream();
  2.      System.IO.TextWriter toWrite =(System.IO.TextWriter) new System.IO.StreamWriter( toCopy);
  3.      toWrite.Write(row["CUSTOMFONT"]);
  4.      toWrite.Flush();
  5.      toCopy.Position = 0;
  6.      XmlSerializer MiniSerializer = new XmlSerializer(typeof(XmlFont));
  7.      XmlFont FontToSerialize = (XmlFont) MiniSerializer.Deserialize(toCopy);
  8.      this.CustomFont= FontToSerialize.ToFont();


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  [C#] Serializer un objet 'Font'

 

Sujets relatifs
Probleme de serialisation d'objetProblème d'objet instancié?
Création d'objet dynamiqueconception classique ou objet ?
Include multiples qui font planter la compilation (Gcc Linux)[C++] objet.membre et objet->membre ?
[Objet]xml et simplexml.la bonne manière de vérifier le type d'un objet ?
Objet sound() dans un clip chargé par loadClipBesoin d'aide sur setInterval et javascript orienté objet...
Plus de sujets relatifs à : [C#] Serializer un objet 'Font'


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR