473,379 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Convert RGB to hexadecimal

I want to convert the value of color.RGB of font object to hexadecimal
value.
How is it possible. Can some one help me.
I want to do it through my code internally without using any third party
tool.

John

Nov 17 '05 #1
4 28989

This is an example function:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>

but also we should handle the transparent color, we could do that by
modifying the function a bit:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
if (System.Drawing.Color.Transparent == color)
return "Transparent";
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"John" <jo**********@hotmail.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
I want to convert the value of color.RGB of font object to hexadecimal
value.
How is it possible. Can some one help me.
I want to do it through my code internally without using any third party
tool.

John

Nov 17 '05 #2

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...

This is an example function:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>

but also we should handle the transparent color, we could do that by
modifying the function a bit:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
if (System.Drawing.Color.Transparent == color)
return "Transparent";
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>

I used your code and it works fine for some sample code, but in my
application I get the color as RGB long value and I try to convert it to
system color using this method

r=(System.Drawing.Color)System.ComponentModel.Type Descriptor.GetConverter(typeof(System.Drawing.Colo r)).ConvertFromString(ppFont.Color.RGB.ToString()) ;

this results in wrong color creation. i.e for blue color it gives red color.

Is there some other method through which I can convert the RGB long value to
color object.

Please help.


Nov 17 '05 #3
You can use the static FromArgb:
System.Drawing.Color color = System.Drawing.Color.FromArgb(longRgb);

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"John" <jo**********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

"Dennis Myrén" <de****@oslokb.no> wrote in message
news:ei**************@tk2msftngp13.phx.gbl...

This is an example function:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>

but also we should handle the transparent color, we could do that by
modifying the function a bit:
<code>
public static string ToHtml ( System.Drawing.Color color )
{
if (System.Drawing.Color.Transparent == color)
return "Transparent";
return string.Concat("#", (color.ToArgb() & 0x00FFFFFF).ToString("X6"));
}
</code>

I used your code and it works fine for some sample code, but in my
application I get the color as RGB long value and I try to convert it to
system color using this method

r=(System.Drawing.Color)System.ComponentModel.Type Descriptor.GetConverter(typeof(System.Drawing.Colo r)).ConvertFromString(ppFont.Color.RGB.ToString()) ;

this results in wrong color creation. i.e for blue color it gives red
color.

Is there some other method through which I can convert the RGB long value
to color object.

Please help.

Nov 17 '05 #4
See ColorTransator.ToHtml or ColorTranslator.FromHtml

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"John" <jo**********@hotmail.com> wrote in message
news:Oe**************@TK2MSFTNGP10.phx.gbl...
I want to convert the value of color.RGB of font object to hexadecimal
value.
How is it possible. Can some one help me.
I want to do it through my code internally without using any third party
tool.

John

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Eric | last post by:
Guys, I have the following hexadecimal string - '\xff\xff\xff' which i need to convert to binary. How would i go about doing this? Eric
2
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For example: Dim outputBank As New BitArray(8) ...
2
by: Phil Stanton | last post by:
When designing a new form or report, the Default ForeColor is often something like -2147483640 which is the colour of Windows text (possibly black) and the default backColor is -2147483643...
9
by: FalkoG | last post by:
Hello colleague I want to convert a floating number for example 5236.9856982 to a hexadecimal number. I'd tried several things but my problem is to convert the position after decimal point. I...
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
13
by: muss | last post by:
how to convert decimal number in a hexadecimal number using C? Please help me. Thanks.
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
1
mahet
by: mahet | last post by:
Anders Jansson was a guest here, and he was having problem: When I try to build the web, this error is displayed: ---> Error 5 Unable to convert input xml file content to a DataSet. Invalid...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
10
by: cmdolcet69 | last post by:
Public ArrList As New ArrayList Public bitvalue As Byte() Public Sub addvalues() Dim index As Integer ArrList.Add(100) ArrList.Add(200) ArrList.Add(300) ArrList.Add(400) ArrList.Add(500)
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.