加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将十六进制字符串转换为颜色

发布时间:2020-12-16 01:51:44 所属栏目:百科 来源:网络整理
导读:我在QueryString中传递十六进制值.我想将其转换为颜色,以在网格视图中的单元格中使用ForeColor.试过System.Drawing.ColorTranslator.From Html()和System.Drawing.Color.FromArgb()没有运气. 我的QueryString是urlencoded所以重要的部分看起来像: QueryStri
我在QueryString中传递十六进制值.我想将其转换为颜色,以在网格视图中的单元格中使用ForeColor.试过System.Drawing.ColorTranslator.From Html()和System.Drawing.Color.FromArgb()没有运气.

我的QueryString是urlencoded所以重要的部分看起来像:

QueryString...&color=%23AA4643

以下是我试过的方法.FromArg:

string sColor = Request.QueryString["color"]; // sColor is now #AA4643
Int32 iColorInt = Convert.ToInt32(sColor,16); //Get error message - Could not find any recognizable digits
Color curveColor = System.Drawing.Color.FromArgb(iColorInt); //Never makes it here

以下是我的尝试.FromHtml:

string sColor = Request.QueryString["color"]; 
System.Drawing.Color myColor = new System.Drawing.Color();
myColor = System.Drawing.ColorTranslator.FromHtml(sColor);

在这种情况下,myColor设置为 – myColor =“{Name = ffaa4643,ARGB =(255,170,70,67)}”

但是当我去使用它时,我收到一个错误:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

任何和所有的帮助非常感谢

解决方法

试试这个:

string sColor = Request.QueryString["color"]; // sColor is now #AA4643
Int32 iColorInt = Convert.ToInt32(sColor.Substring(1),16); 
Color curveColor = System.Drawing.Color.FromArgb(iColorInt);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读