ColorTranslator.FromHtml(String) 方法

定義

將 HTML 色彩表示轉換為 GDI+ Color 結構。Translates an HTML color representation to a GDI+ Color structure.

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 色彩字串表示。The string representation of the Html color to translate.

傳回

Color 結構,表示要轉換的 HTML 色彩;如果是 EmptyhtmlColor 則為 nullThe Color structure that represents the translated HTML color or Empty if htmlColor is null.

例外狀況

htmlColor 不是有效的 HTML 色彩名稱。htmlColor is not a valid HTML color name.

範例

下列範例是針對與 Windows Forms 搭配使用所設計,而且需要 PaintEventArgse,這是 Paint 事件處理常式的參數。The following example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 程式碼會將 HTML 色彩名稱轉譯為 Color 結構,然後使用該色彩來填滿一個矩形。The code translates an HTML color name to a Color structure, and then uses that color to fill a rectangle.

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 結構。This method translates a string representation of an HTML color name, such as Blue or Red, to a GDI+ Color structure.

適用於