using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Markup; using System.Windows.Media; using System.Xml; internal static class BrushExtensions { internal static String ConvertToString(this Brush brush) { XmlWriterSettings settings = new XmlWriterSettings(); // You might want to wrap these in #if DEBUG's settings.Indent = true; settings.NewLineOnAttributes = true; // this gets rid of the XML version settings.ConformanceLevel = ConformanceLevel.Fragment; // buffer to a stringbuilder StringBuilder sb = new StringBuilder(); XmlWriter writer = XmlWriter.Create(sb, settings); // Need moar documentation on the manager, plox MSDN XamlDesignerSerializationManager manager = new XamlDesignerSerializationManager(writer); manager.XamlWriterMode = XamlWriterMode.Expression; // its extremely rare for this to throw an exception XamlWriter.Save(brush, manager); return sb.ToString(); } internal static Brush ToBrush(this String str) { XmlDocument doc = new XmlDocument(); // may throw XmlException doc.LoadXml(str); // may throw XamlParseException return XamlReader.Load(new XmlNodeReader(doc)) as Brush; } }