.NET - 将颜色名称字符串转换为 System.Drawing.Color

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/579528/
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-09-03 12:11:25  来源:igfitidea点击:

.NET - Converting Color Name Strings into System.Drawing.Color

.netcolorssystem.drawing.color

提问by BuddyJoe

What is the best way to turn strings like "red", "green", "yellow", "aliceblue", etc... into the actual System.Drawing.Color value?

将“red”、“green”、“yellow”、“aliceblue”等字符串转换为实际 System.Drawing.Color 值的最佳方法是什么?

I was looking at reflection and something about that didn't seem right.

我正在查看反思,但似乎有些不对劲。

回答by Alex Reitbort

You can use Color.FromName()

您可以使用 Color.FromName()

回答by Moose

System.Drawing.Color has a static method:

System.Drawing.Color 有一个静态方法:

public static Color FromName(string name)

Use it like so:

像这样使用它:

   Color c = Color.FromName("AliceBlue")

回答by DavidN

System.Drawing.Color.FromName("Red");

System.Drawing.Color.FromName("红色");