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 颜色的字符串表示形式。

返回

表示已翻译的 HTML 颜色或 ColorEmpty 结构(如果 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 颜色名称(如 Blue 或 Red)的字符串表示形式转换为 GDI+ Color 结构。

适用于