Image (supports XML serialization), note that C # Native image class is not XML serialized

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.