C# 如何将十六进制 #FFFFFF 转换为 System.Drawing.Color

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12155811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:17:46  来源:igfitidea点击:

How to convert Hexadecimal #FFFFFF to System.Drawing.Color

c#asp.net

提问by user1531040

Possible Duplicate:
How to get Color from Hex color code using .NET?

可能的重复:
如何使用 .NET 从十六进制颜色代码中获取颜色?

I want to convert a string like #FFFFFFto System.Drawing.Color. How do you do that?

我想将字符串转换#FFFFFFSystem.Drawing.Color. 你是怎样做的?

采纳答案by varg

string hex = "#FFFFFF";
Color _color = System.Drawing.ColorTranslator.FromHtml(hex);

Note: the hash is important!

注意:哈希很重要!

回答by codeteq

Remove the '#' and do

删除“#”并执行

Color c = Color.FromArgb(int.Parse("#FFFFFF".Replace("#",""),
                         System.Globalization.NumberStyles.AllowHexSpecifier));

回答by SidAhmed

You can do

你可以做

var color =  System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

Or this (you will need the System.Windows.Medianamespace)

或者这个(你需要System.Windows.Media命名空间)

var color = (Color)ColorConverter.ConvertFromString("#FFFFFF");