[英]System.Drawing.Color ToKnownName
I need a opposite function from System.Drawing.Color.FromKnownName that converts System.Drawing.Color.Red to "Red" or "red". 我需要一个与System.Drawing.Color.FromKnownName相反的函数,该函数将System.Drawing.Color.Red转换为“ Red”或“ red”。
To provide example code: 提供示例代码:
private static XElement BlipToXml(Blip blip)
{
var tmp = new XElement("Blip",
new XAttribute("X", blip.Position.X),
new XAttribute("Y", blip.Position.Y),
new XAttribute("Z", blip.Position.Z),
new XAttribute("color", blip.Color.), <-- This is where i need the ToKnownName
new XAttribute("transparency", blip.Alpha),
new XAttribute("sprite", blip.Sprite));
tmp.SetValue(blip.Name);
return tmp;
}
private static Blip XmlToBlip(XElement xml)
{
var x = float.Parse(xml.Attribute("X").ToString());
var y = float.Parse(xml.Attribute("Y").ToString());
var z = float.Parse(xml.Attribute("Z").ToString());
var coords = new Vector3(x,y,z);
var tmp = new Blip(coords);
tmp.Color = System.Drawing.Color.FromName(xml.Attribute("color").ToString());
tmp.Alpha = float.Parse(xml.Attribute("transparency").ToString());
tmp.Sprite = (BlipSprite)Enum.Parse(typeof(BlipSprite), xml.Attribute("sprite").ToString());
tmp.Name = xml.Value;
return tmp;
}
This method uses reflection to examine the predefined colors on the Color class and compare them against the color passed in as an argument. 此方法使用反射来检查Color类上的预定义颜色,并将它们与作为参数传入的颜色进行比较。
private static String GetColorName(Color color)
{
var predefined = typeof(Color).GetProperties(BindingFlags.Public | BindingFlags.Static);
var match = (from p in predefined where ((Color)p.GetValue(null, null)).ToArgb() == color.ToArgb() select (Color)p.GetValue(null, null));
if (match.Any())
return match.First().Name;
return String.Empty;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.