Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Developing a color palette by passing hex codes using winforms
Posted
Updated 19-Apr-15 21:01pm
v2
Comments
Sergey Alexandrovich Kryukov 20-Apr-15 3:01am    
What do you mean "how"? By doing appropriate software development work. This is not a real question.
Your post does not provide enough detail on required behavior and does not explain any problems.
What have you tried so far?
—SA
Member 11497540 20-Apr-15 3:26am    
In a form i have to load picture boxex dynamically.In tha picture box I have to display colors by passing Hex codes.Instead of picture box we can use button also.
Bjørn 20-Apr-15 4:45am    
So what exactly is the problem you're having? Converting HEX codes to a System.Drawing.Color? or creating the PictureBoxes or Buttons dynamically? What have you tried yet?
Member 11497540 20-Apr-15 6:28am    
to display that color on that button by passing color is the problem

1 solution

Okay, so, from what I understand, the problem is parsing the HEX color? That's quite easy in C#:

C#
public Color FromHexValue(string value)
{
    // Make sure the alpha is either passed or set to full
    if (value.Length == 6) value = "FF" + value;
    // Convert the hex value to it's int value
    return Color.FromArgb(Convert.ToInt32(value, 16));
}


This example does NOT check for wrong values so be careful to check that first. Like this the method can handle 6 (RRGGBB) or 8 (AARRGGBB) hexadecimal character values like:

FF0000
FFFF0000


Both are the same Color: Red.

I hope that helps.

Best regards!

(\/)
( . .)
c(")(") Bjørn
 
Share this answer
 
Comments
Member 11497540 21-Apr-15 1:12am    
how to make that color display on a button or picturebox?
Member 11497540 21-Apr-15 3:12am    
simply i want to develop a colorpalette by passing hex codes.Not the default color dialog color palette.
Bjørn 21-Apr-15 7:59am    
You can set the property "BackColor" of the Button like this:

button1.BackColor = Color.Red;

or when using the function above:

button1.BackColor = FromHexValue("FF0000");
Member 11497540 22-Apr-15 4:35am    
Thank you very much i got that.But how to apply that button color to a picture.Can you help me plz.
Bjørn 22-Apr-15 7:35am    
If you just mean making a PictureBox shoing the color, you can easily set the same property "BackColor":

pictureBox1.BackColor = Color.Red;

If you really want to use a picture (as in using a Bitmap) you'll have to create one manually:

public Bitmap GetColoredBitmap(Color c, int width, int height)
{
Bitmap bmp = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(c);
}
return bmp;
}

Then you can set the "Image" property of the PictureBox with that Bitmap. Use the PictureBox's "Width" and "Height" properties to make the Bitmap the same size as the PictureBox:

pictureBox1.Image = GetColoredBitmap(Color.Red, pictureBox1.Width, pictureBox1.Height);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900