十六进制和RGB颜色互转
颜色的十六进制和RGB格式进行相互转化
public class ColorConvert
{
#region [颜色:16进制转成RGB]
///
/// [颜色:16进制转成RGB]
///
/// 设置16进制颜色 [返回RGB]
///
public static System.Drawing.Color colorHx16toRGB(string strHxColor)
{
try
{
if (strHxColor.Length == 0)
{//如果为空
return System.Drawing.Color.FromArgb(0, 0, 0);//设为黑色
}
else
{//转换颜色
return System.Drawing.Color.FromArgb(System.Int32.Parse(strHxColor.Substring(1, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(3, 2), System.Globalization.NumberStyles.AllowHexSpecifier), System.Int32.Parse(strHxColor.Substring(5, 2), System.Globalization.NumberStyles.AllowHexSpecifier));
}
}
catch
{//设为黑色
return System.Drawing.Color.FromArgb(0, 0, 0);
}
}
public static System.Drawing.Color GetColor(string val)
{
val = val.Trim();
if (val.Substring(0, 1) == "#")
return colorHx16toRGB(val);
else
return Color.FromName(val);
}
#endregion
#region [颜色:RGB转成16进制]
///
/// [颜色:RGB转成16进制]
///
/// 红 int
/// 绿 int
/// 蓝 int
///
public static string colorRGBtoHx16(int R, int G, int B)
{
return System.Drawing.ColorTranslator.ToHtml(System.Drawing.Color.FromArgb(R, G, B));
}
#endregion
}
LINQ XML控制类
一个非常方便控制XML的操作类,应用了LINQ
包括XML的读写,节点的读写和添加删除等基本操作。
public class XmlOperator
{
#region 属性
private string _strPath = "";
/// <summary>
/// 文件路径
/// Open时或者Saveas时改变
/// </summary>
public string Path
{
get { return _strPath; }
}
private XmlDocument _xmlDocumentdoc = null;
/// <summary>
/// 获取xml对象
/// </summary>
public XmlDocument xmlDocument
{
get { return this._xmlDocumentdoc; }
}
#endregion
public XmlOperator()
{
}
public XmlOperator(string strPath)
{
_xmlDocumentdoc = OpenXml(strPath);
}
#region 方法
/// <summary>
/// 打开文件
/// </summary>
/// <param name="strPath"></param>
public void XmlOpen(string strPath)
{
XmlDocument xdtdoc = new XmlDocument();
xdtdoc = OpenXml(strPath);
if (xdtdoc == null)
throw new Exception("打开文件失败,请检查文件路径");
this._xmlDocumentdoc = xdtdoc;
}
public void XmlSave()
{
if (this._strPath == "")
throw new Exception("文件路径为空或者不存在");
SaveXml(this._xmlDocumentdoc, this._strPath);
}
public void XmlSaveAs(string strPath)
{
SaveXml(this._xmlDocumentdoc, strPath);
}
/// <summary>
/// 将xml对象转化成DataTable
/// </summary>
/// <returns></returns>
public DataTable CoverToTable()
{
if (this._xmlDocumentdoc != null)
{
DataSet dsxml = null;
using (XmlReader reader = new XmlNodeReader(this._xmlDocumentdoc.DocumentElement))
{
dsxml = new DataSet();
dsxml.ReadXml(reader);
reader.Close();
}
if (dsxml != null)
return dsxml.Tables[0];
}
return null;
}