ARGB颜色转换帮助类(最后附上全部代码)
效果如下
颜色转换帮助类 代码
using System;
using System.Collections.Generic;
using System.Drawing;
/// <summary>
/// 颜色转换帮助类
/// </summary>
public static class ColorHelpClass
{
/// <summary>
/// 将ARGB颜色转换成string
/// </summary>
/// <param name="color">Color 类型的 ARGB颜色</param>
/// <returns>返回string类型的ARGB颜色</returns>
public static string GetStingArgb(Color color)
{
string Argb = color.A.ToString() + "," + color.R.ToString() + "," + color.G.ToString() + "," + color.B.ToString();
return Argb;
}
/// <summary>
/// 将string类型的ARGB颜色 转换成 Color 类型
/// </summary>
/// <param name="strcolor">string类型的ARGB颜色,以逗号分隔。如:255,255,128,255</param>
/// <returns>返回 Color 类型的ARGB颜色</returns>
public static Color GetColorArgb(string strcolor)
{
var ddd = strcolor.Split(',');
List<string> list = new List<string>(strcolor.Split(','));
int A = Convert.ToInt32(list[0]);
int R = Convert.ToInt32(list[1]);
int G = Convert.ToInt32(list[2]);
int B = Convert.ToInt32(list[3]);
return Color.FromArgb(A, R, G, B);
}
}
全部 代码
using System;
using System.Drawing;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
string color = "255,255,128,255";//string类型的ARGB颜色
Color ccolor = ColorHelpClass.GetColorArgb(color);//string 转 Color
string scolor = ColorHelpClass.GetStingArgb(ccolor);//Color 转 string
Console.WriteLine("Color类型的ARGB:" + ccolor);
Console.WriteLine("string类型的ARGB:" + scolor);
}
}
/// <summary>
/// 颜色转换帮助类
/// </summary>
public static class ColorHelpClass
{
/// <summary>
/// 将ARGB颜色转换成string
/// </summary>
/// <param name="color">Color 类型的 ARGB颜色</param>
/// <returns>返回string类型的ARGB颜色</returns>
public static string GetStingArgb(Color color)
{
string Argb = color.A.ToString() + "," + color.R.ToString() + "," + color.G.ToString() + "," + color.B.ToString();
return Argb;
}
/// <summary>
/// 将string类型的ARGB颜色 转换成 Color 类型
/// </summary>
/// <param name="strcolor">string类型的ARGB颜色,以逗号分隔。如:255,255,128,255</param>
/// <returns>返回 Color 类型的ARGB颜色</returns>
public static Color GetColorArgb(string strcolor)
{
var ddd = strcolor.Split(',');
List<string> list = new List<string>(strcolor.Split(','));
int A = Convert.ToInt32(list[0]);
int R = Convert.ToInt32(list[1]);
int G = Convert.ToInt32(list[2]);
int B = Convert.ToInt32(list[3]);
return Color.FromArgb(A, R, G, B);
}
}
}