1 /// <summary>2 ///Image (XML serialization supported)3 /// </summary>4[XmlRoot ("Xmlimage")]5 Public classxmlimage:ixmlserializable6 {7 PublicSystem.Drawing.Image Image {Get;Set; }8 9 #regionconstructor functionTen One Publicxmlimage () A { } - - protectedxmlimage (SerializationInfo info, StreamingContext context) the { } - #endregionconstructor function - - #regionIXmlSerializable Members + PublicXmlSchema GetSchema () =NULL; - + /// <summary> A ///generate the object from the object's XML representation (deserialization) at /// </summary> - /// <param name= "XR" ></param> - Public voidReadXml (XmlReader XR) - { - if(XR. Isemptyelement) - return; in XR. Readtofollowing (nameof (xmlimage)); -Image =frombase64string (XR. Readelementcontentasstring ()); to XR. Readendelement (); + } - the /// <summary> * ///convert an object to its XML representation (serialized) $ /// </summary>Panax Notoginseng /// <param name= "XW" ></param> - Public voidWriteXml (XmlWriter XW) the { + XW. WriteStartElement (nameof (xmlimage)); A XW. WriteValue (ToBase64String (Image, imageformat.png)); the XW. WriteEndElement (); + } - #endregionIXmlSerializable Members $ $ Private Static stringtobase64string (System.Drawing.Image img, imageformat format) - { - if(img! =NULL) the { - using(varms =NewMemoryStream ())Wuyi { the img. Save (MS, format); - byte[] buffer =Ms. ToArray (); Wu returnconvert.tobase64string (buffer); - } About } $ return string. Empty; - } - - Private StaticSystem.Drawing.Image FromBase64String (stringbase64str) A { +System.Drawing.Image img =NULL; the using(varms =NewMemoryStream ()) - { $ Try the { the varBuffer =convert.frombase64string (BASE64STR); theMs. Write (Buffer,0, buffer. Length); theIMG =System.Drawing.Image.FromStream (MS); - } in Catch the { the //ignored About } the } the returnimg; the } +}
Image (supports XML serialization), note that C # Native image class is not XML serialized