How to create a System.Drawing.Color from its hexadecimal RGB string?

前端 未结 6 1540
-上瘾入骨i
-上瘾入骨i 2020-12-30 18:37

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

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-30 19:01

    Use the ColorConverter class:

    var converter = System.ComponentModel.TypeDescriptor.GetConverter( typeof( Color ) );
    color = converter.ConvertFromString( "#FF00FF" );
    

    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.

提交回复
热议问题