Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining colour hex value for image in C#

Is it possible to determine the hexadecimal colour value for an image in C#, if the image is a single block colour?

I have a load of colour chart images, and need to put them into a table with the requisite hex value attached. Unfortunately, the file name is useless to determine this.

Regards

Moo


1 Answers

I would use Bitmap.GetPixel to get the color and then convert it to hex using ColorTranslator.ToHtml.


// Load the bitmap
Bitmap bitmap = new Bitmap("test.gif");

// Get color from top left corner            
Color color = bitmap.GetPixel(0, 0);

// Convert it to hex
String htmlColor = System.Drawing.ColorTranslator.ToHtml(color);


like image 195
Petr Havlicek Avatar answered Nov 23 '25 18:11

Petr Havlicek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!