I want to create a System.Drawing.Color
from a value like #FF00FF
or FF00FF
without needing to write code for that. There is any .NET built-in parser for that?
相关问题
- Generic Generics in Managed C++
- Correctly parse PDF paragraphs with Python
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
The FromName method worked for me
You can use the System.Drawing.ColorTranslator static method FromHtml.
use:
It is rather easy when you use the Convert-Class. The
ToInt32
function has an overload with a second parameter which represents the base the string is in.Use the ColorConverter class:
This can also convert from the standard named colors e.g. ConvertFromString( "Blue" )
See here for a discussion of the standard .NET type conversion mechanisms.
If the color you want to use is a constant, in C# use
System.Drawing.Color.FromArgb (0xFF00FF)
. That is slightly faster thanSystem.Drawing.Color.FromName
orSystem.Drawing.Color.FromHtml
, since the parsing from a string to integer is done at compile time rather than at runtime.