ColorTranslator.FromHtml(String) 方法

定义

将 HTML 颜色表示形式转换为 GDI+ Color 结构。

public:
 static System::Drawing::Color FromHtml(System::String ^ htmlColor);
public static System.Drawing.Color FromHtml(string htmlColor);
static member FromHtml : string -> System.Drawing.Color
Public Shared Function FromHtml (htmlColor As String) As Color

参数

htmlColor
String

要翻译的 Html 颜色的字符串表示形式。

返回

表示Color已转换的 HTML 颜色或Empty如果为 htmlColornull 的结构。

例外

htmlColor 不是有效的 HTML 颜色名称。

示例

以下示例旨在与 Windows 窗体一起使用,它需要 PaintEventArgse此参数作为事件处理程序的参数 Paint 。 该代码将 HTML 颜色名称 Color 转换为结构,然后使用该颜色填充矩形。

public:
   void FromHtml_Example( PaintEventArgs^ e )
   {
      // Create a string representation of an HTML color.
      String^ htmlColor = "Blue";

      // Translate htmlColor to a GDI+ Color structure.
      Color myColor = ColorTranslator::FromHtml( htmlColor );

      // Fill a rectangle with myColor.
      e->Graphics->FillRectangle( gcnew SolidBrush( myColor ), 0, 0, 100, 100 );
   }
public void FromHtml_Example(PaintEventArgs e)
{
    // Create a string representation of an HTML color.
    string htmlColor = "Blue";
             
    // Translate htmlColor to a GDI+ Color structure.
    Color myColor = ColorTranslator.FromHtml(htmlColor);
             
    // Fill a rectangle with myColor.
    e.Graphics.FillRectangle( new SolidBrush(myColor), 0, 0, 
        100, 100);
}
Public Sub FromHtml_Example(ByVal e As PaintEventArgs)

    ' Create a string representation of an HTML color.
    Dim htmlColor As String = "Blue"

    ' Translate htmlColor to a GDI+ Color structure.
    Dim myColor As Color = ColorTranslator.FromHtml(htmlColor)

    ' Fill a rectangle with myColor.
    e.Graphics.FillRectangle(New SolidBrush(myColor), 0, 0, 100, 100)
End Sub

注解

此方法将 HTML 颜色名称(如蓝色或红色)的字符串表示形式转换为 GDI+ Color 结构。

适用于