Get Color from String
The String represents text as a sequence of UTF-16 code units. The
String is a sequential collection of characters that is used to represent
text. The String is a sequential collection of System.Char objects.
The following .net c# tutorial code demonstrates how we can get a Color instance from a String object. So, in this .net c# tutorial code we will convert a String instance that contains a color name into a Color object. The Color represents an ARGB (alpha, red, green, blue) color.
The Color FromName() method creates a Color structure from the specified name of a predefined color. The Color FromName(string name) has a required parameter named ‘name’. The Color FromName(string name) method's name parameter is a String that is the name of a predefined color. The valid names are the same as the names of the elements of the KnownColor enumeration.
The Color FromName() method returns a Color that this method creates. If the name parameter is not the valid name of a specified color then the method creates a Color structure that has an ARGB value of 0. All ARGB components are 0. So, using this Color FromName(String) method the .net c# developers can get a Color object from a String instance.
The following .net c# tutorial code demonstrates how we can get a Color instance from a String object. So, in this .net c# tutorial code we will convert a String instance that contains a color name into a Color object. The Color represents an ARGB (alpha, red, green, blue) color.
The Color FromName() method creates a Color structure from the specified name of a predefined color. The Color FromName(string name) has a required parameter named ‘name’. The Color FromName(string name) method's name parameter is a String that is the name of a predefined color. The valid names are the same as the names of the elements of the KnownColor enumeration.
The Color FromName() method returns a Color that this method creates. If the name parameter is not the valid name of a specified color then the method creates a Color structure that has an ARGB value of 0. All ARGB components are 0. So, using this Color FromName(String) method the .net c# developers can get a Color object from a String instance.
string-to-color.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 a string variable.
string stringOfColor = "Crimson";
string stringOfColor2 = "LawnGreen";
Label1.Text = "string of color1..................<br />";
Label1.Text += stringOfColor;
Label1.Text += "<br /><br />string of color2..................<br />";
Label1.Text += stringOfColor2;
//this line create a color from string value;
Color c = Color.FromName(stringOfColor);
Color c2 = Color.FromName(stringOfColor2);
//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 - string to color</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - string to color
</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="string to color"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>