c# - How to convert a hex string to color

Color from hex string
color-from-hex-string.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System.Drawing" %>
  
<!DOCTYPE html>    
<script runat="server">  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
        //this section create string variables.  
        string hexString = "#FF0000";
        string hexString2 = "#F5DEB3";

        Label1.Text = "hexadecimal color string..................<br />";
        Label1.Text += hexString;

        Label1.Text += "<br /><br />hexadecimal color code2..................<br />";
        Label1.Text += hexString2;

        //this line create a red color from hexadecimal color string.
        Color c = ColorTranslator.FromHtml(hexString);

        //this line create a wheat color from hexadecimal color code.
        Color c2 = ColorTranslator.FromHtml(hexString2);

        //applying text color to label control
        Label1.ForeColor = c;
        
        //applying background color to label control.
        Label1.BackColor = c2;
        
        //applying new font size of label.
        Label1.Font.Size = FontUnit.XXLarge;
    }    
</script>    
    
<html xmlns="http://www.w3.org/1999/xhtml">    
<head id="Head1" runat="server">    
    <title>c# example - color from hex string</title>    
</head>    
<body>    
    <form id="form1" runat="server">    
    <div>    
        <h2 style="color:MidnightBlue; font-style:italic;">    
            c# example - color from hex string
        </h2>    
        <hr width="550" align="left" color="Gainsboro" />    
        <asp:Label     
            ID="Label1"     
            runat="server"    
            Font-Size="Large"  
            >    
        </asp:Label>    
        <br /><br />  
        <asp:Button     
            ID="Button1"     
            runat="server"     
            Text="color from hex string"    
            OnClick="Button1_Click"  
            Height="40"    
            Font-Bold="true"    
            />    
    </div>    
    </form>    
</body>    
</html>
More c# examples