aiden87
aiden87

Reputation: 969

How to select and get color in asp.net

As there is no integrated attribute for color in asp.net (atleast i haven't found it), i wonder, how do you guys select color?

Let's say i want to create a drawing using bitmap and i want to get background color from the user who selects it using the application.

I've done some code behind, but thing doesn't work, as i manually type in colors like #000 or #fff (using textbox)

app.aspx

<label>
<span>Background color</span>
<asp:TextBox ID="inp_bgColor" Width="125px" runat="server"></asp:TextBox>
</label><span style="color:red"><asp:Literal ID="error_bg" runat="server"></asp:Literal></span><br /><br /> 

and app.aspx.cs

Color txtClr = Color.FromName(inp_bgColor.Text);

I've noticed that System.Drawing.Color is type of ARGB color, so how do i do this?

Thanks!

Upvotes: 0

Views: 7687

Answers (3)

Roi Snir
Roi Snir

Reputation: 441

The Color.FromName method gets colors from the KnownColor enumeration (see know color table
Try using the Color.FromARGB method see here For simple user color picking you can use the color dialog like this:

// Show the color dialog.
ColorDialog colorDialog1=new ColorDialog();
DialogResult result = colorDialog1.ShowDialog();
// See if user pressed ok.
Color selectedColor;
if (result == DialogResult.OK)
{
    selectedColor = colorDialog1.Color;
}

Upvotes: 1

abdul
abdul

Reputation: 1592

You can use hex colors like this:

string hex = "#FF3FF3";
Color _color = System.Drawing.ColorTranslator.FromHtml(hex);

Upvotes: 2

M. Wiśnicki
M. Wiśnicki

Reputation: 6203

You can get color using .Attributes.CssStyle["color"], but your element must have css style property like color set.

 inp_bgColor.Attributes.CssStyle["color"]

And you can use this way to convert

Color _color = System.Drawing.ColorTranslator.FromHtml("#FFFFFF");

Upvotes: 0

Related Questions