转换此“颜色[a = 255,R = 128,G = 0,B = 255]” C#中的颜色值 [英] Convert this"color [a=255, R=128, G=0, B=255] " value to color in C#

查看:546
本文介绍了转换此“颜色[a = 255,R = 128,G = 0,B = 255]” C#中的颜色值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#winform应用程序,我将颜色存储在数据库

hi i'm working on c# winform application and i store the color in databse "

Color [A=255, R=128, G=0, B=255]

在这种形式下我现在要将其转换回颜色。任何一个告诉我我该怎么做



我尝试了什么:



我从sql server 2014获得字符串形式的颜色

"in this form now i want to convert this to back in color .any one tell me how can i do this

What I have tried:

I'm getting color in string form from sql server 2014

string bakcolo=row["Color"].ToString();
            // and try this method to convert string in color but its not working
               Color mycolor = ColorTranslator.FromHtml(bakcolo);
              Colorbutton1.BackColor=Color.FromName(bakcolo);

推荐答案

阅读: c# - 将字符串转换为Color - Stack Overflow [ ^ ]


为什么要将数据库中的颜色值存储为字符串?它适合作为原生值的无符号整数...然后再次获得一个颜色只是一件简单的事情:

Why are you storing a Color value in a DB as a string? It fits in an unsigned integer as a "native" value ... and then it's just a simple matter to get a Color back again:
Color c = Color.AliceBlue;
int i = c.ToArgb();
c = Color.FromArgb(i);



玩字符串既低效又麻烦:


To play with strings is both inefficient, and cumbersome:

string s = "Color [A=255, R=128, G=0, B=255]";
Match m = Regex.Match(s, @"A=(?<Alpha>\d+),\s*R=(?<Red>\d+),\s*G=(?<Green>\d+),\s*B=(?<Blue>\d+)");
if (m.Success)
    {
    int alpha = int.Parse(m.Groups["Alpha"].Value);
    int red = int.Parse(m.Groups["Red"].Value);
    int green = int.Parse(m.Groups["Green"].Value);
    int blue = int.Parse(m.Groups["Blue"].Value);
    Color c = Color.FromArgb(alpha, red, green, blue);
    ...
    }


这篇关于转换此“颜色[a = 255,R = 128,G = 0,B = 255]” C#中的颜色值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆