473,385 Members | 1,449 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,385 software developers and data experts.

convert Uint32 to System.drawing.color

Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv

Feb 21 '07 #1
11 15316
More details about my problem:
I want to change the color of label (Label1.ForeColor which is
system.drawing.color).
I want to take the color from object which has a Uint32 color

Yaniv כתב:
Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv
Feb 21 '07 #2
More details about my problem:
I want to change the color of label (Label1.ForeColor which is
system.drawing.color).
I want to take the color from object which has a Uint32 color

Yaniv כתב:
Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv
Feb 21 '07 #3
Yaniv wrote:
Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv
If the integer is representing an argb (alpha+rgb) value, then you can
use the Color.FromArgb(int) method. If it only represents an rgb value,
then you can use Color.FromArgb(int, int), where the first integer is
the alpha value, which you should set to 255 to get a solid color.

--
Göran Andersson
_____
http://www.guffa.com
Feb 21 '07 #4

Göran Andersson כתב:
Yaniv wrote:
Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv

If the integer is representing an argb (alpha+rgb) value, then you can
use the Color.FromArgb(int) method. If it only represents an rgb value,
then you can use Color.FromArgb(int, int), where the first integer is
the alpha value, which you should set to 255 to get a solid color.

--
Göran Andersson
_____
http://www.guffa.com
Hi

The first option you wrote is not working since the value is Uint32
and not integer as you wrote (I'v got the message "Value of type
'System.UInt32' cannot be converted to 'Integer')

The second option doesn't exist in VB.NET at all (Anyway I can't
convert Uint32 to int.....)
actually I dont know if the Uint32 color is represent argb color or
only rgb color...

Feb 22 '07 #5
Yaniv wrote:
Göran Andersson כתב:
>Yaniv wrote:
>>Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv
If the integer is representing an argb (alpha+rgb) value, then you can
use the Color.FromArgb(int) method. If it only represents an rgb value,
then you can use Color.FromArgb(int, int), where the first integer is
the alpha value, which you should set to 255 to get a solid color.

--
Göran Andersson
_____
http://www.guffa.com

Hi

The first option you wrote is not working since the value is Uint32
and not integer as you wrote (I'v got the message "Value of type
'System.UInt32' cannot be converted to 'Integer')
You can just cast the UInt32 to an Int32:

Color color = Color.FromArgb((int)colorValue);
The second option doesn't exist in VB.NET at all (Anyway I can't
convert Uint32 to int.....)
Yes, you are right. There is no such overload to the method. I was
thinking of the Color.FromArgb(int, Color) overload:

Color color = Color.FromArgb(255, Color.FromArgb((int)colorValue));
actually I dont know if the Uint32 color is represent argb color or
only rgb color...
That will be obvious when you try to use it. If it's an rgb value the
top eight bits will be zero, which when used as alpha will make a
completely transparent color.

--
Göran Andersson
_____
http://www.guffa.com
Feb 22 '07 #6
Göran Andersson wrote:
Yaniv wrote:
>Göran Andersson כתב:
>>Yaniv wrote:
Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv

If the integer is representing an argb (alpha+rgb) value, then you can
use the Color.FromArgb(int) method. If it only represents an rgb value,
then you can use Color.FromArgb(int, int), where the first integer is
the alpha value, which you should set to 255 to get a solid color.

--
Göran Andersson
_____
http://www.guffa.com

Hi

The first option you wrote is not working since the value is Uint32
and not integer as you wrote (I'v got the message "Value of type
'System.UInt32' cannot be converted to 'Integer')

You can just cast the UInt32 to an Int32:

Color color = Color.FromArgb((int)colorValue);
Sorry, I gave you C# examples.

In VB.NET:

Dim color As Color = Color.FromArgb(CType(colorValue, Int32))
>The second option doesn't exist in VB.NET at all (Anyway I can't
convert Uint32 to int.....)

Yes, you are right. There is no such overload to the method. I was
thinking of the Color.FromArgb(int, Color) overload:

Color color = Color.FromArgb(255, Color.FromArgb((int)colorValue));
Dim color As Color = Color.FromArgb(255,
Color.FromArgb(CType(colorValue, Int32)))
>actually I dont know if the Uint32 color is represent argb color or
only rgb color...

That will be obvious when you try to use it. If it's an rgb value the
top eight bits will be zero, which when used as alpha will make a
completely transparent color.
--
Göran Andersson
_____
http://www.guffa.com
Feb 22 '07 #7
"Yaniv" <el******@gmail.comschrieb
Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv

Dim u As UInt32 = &HFFFFFFFFUI
Dim c As Color

c = Color.FromArgb( _
BitConverter.ToInt32(BitConverter.GetBytes(u), 0) _
)

Armin

Feb 22 '07 #8
"Göran Andersson" <gu***@guffa.comschrieb
>You can just cast the UInt32 to an Int32:

Color color = Color.FromArgb((int)colorValue);

Sorry, I gave you C# examples.

In VB.NET:

Dim color As Color = Color.FromArgb(CType(colorValue, Int32))
This doesn't work if colorvalue>int32.maxvalue, which is usually the case if
the alpha value 127.

Armin

Feb 22 '07 #9
Armin Zingler wrote:
"Göran Andersson" <gu***@guffa.comschrieb
>>You can just cast the UInt32 to an Int32:

Color color = Color.FromArgb((int)colorValue);

Sorry, I gave you C# examples.

In VB.NET:

Dim color As Color = Color.FromArgb(CType(colorValue, Int32))

This doesn't work if colorvalue>int32.maxvalue, which is usually the
case if the alpha value 127.
Yes, you are right. I tried this in VB, and it won't convert an UInt32
to an Int32 without checking for overflow. It works just fine in C#.
Some say that the CType function in VB is the same as casting in C#, but
obviously it isn't.

I also tried CInt, Convert.ToInt32 and DirectCast, but they all do
overflow checking.

I guess you have to chop up the value in pieces to handle it:

Dim alpha As Integer = colorCode >24
Dim code As Integer = colorCode And &HFFFFFF
Dim c As Color = Color.FromArgb(alpha, Color.FromArgb(code))

or:

Dim alpha As Integer = colorCode >24
Dim red As Integer = (colorCode >16) And &HFF
Dim green As Integer = (colorCode >8) And &HFF
Dim blue As Integer = colorCode And &HFF

Dim c As Color = Color.FromArgb(alpha, red, green, blue)

--
Göran Andersson
_____
http://www.guffa.com
Feb 22 '07 #10
"Göran Andersson" <gu***@guffa.comschrieb
Armin Zingler wrote:
"Göran Andersson" <gu***@guffa.comschrieb
You can just cast the UInt32 to an Int32:

Color color = Color.FromArgb((int)colorValue);
>
Sorry, I gave you C# examples.
>
In VB.NET:
>
Dim color As Color = Color.FromArgb(CType(colorValue, Int32))
This doesn't work if colorvalue>int32.maxvalue, which is usually
the case if the alpha value 127.

Yes, you are right. I tried this in VB, and it won't convert an
UInt32 to an Int32 without checking for overflow.
I had to try it either before because I wasn't sure. :-)
It works just fine
in C#. Some say that the CType function in VB is the same as casting
in C#, but obviously it isn't.

I also tried CInt, Convert.ToInt32 and DirectCast, but they all do
overflow checking.
Right. Type casting is only valid if one type is derived from the other.
CType does type casting if possible. If not, as in this case, it converts.
Conversion includes overflow checking.
I guess you have to chop up the value in pieces to handle it:

Dim alpha As Integer = colorCode >24
Dim code As Integer = colorCode And &HFFFFFF
Dim c As Color = Color.FromArgb(alpha, Color.FromArgb(code))

or:

Dim alpha As Integer = colorCode >24
Dim red As Integer = (colorCode >16) And &HFF
Dim green As Integer = (colorCode >8) And &HFF
Dim blue As Integer = colorCode And &HFF

Dim c As Color = Color.FromArgb(alpha, red, green, blue)
...and with Option Strict On:

Dim alpha As Integer = CInt(colorCode >24)
Dim red As Integer = CInt(colorCode >16) And &HFF
Dim green As Integer = CInt(colorCode >8) And &HFF
Dim blue As Integer = CInt(colorCode And &HFF)


I think, in Framework 2.0, MSFT should have added an overload of FromARGB
that expects an UInteger, and one overload that uses Byte as the type for A,
R, G, B. Don't know why it's Integer at all (already in <2.0) because
values>255 and <0 don't work anyway.
Armin

Feb 22 '07 #11
On Feb 21, 5:00 am, "Yaniv" <elgar...@gmail.comwrote:
Hi

How can I convert Uint32 variable to System.drawing.color ??

Thanks in advanced
Yaniv
A side note: Here's a nice web page with all the colors displayed as
color patches:
http://www.pardesiservices.com/softo...colorchart.asp

Feb 24 '07 #12

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

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
0
by: Nicolas Guilhot | last post by:
Hi all ! I have a multi-page Tiff image file that I want to convert to PDF. To do so I am using iText library. The conversion is working, but the code execution is very different according to...
11
by: Altramagnus | last post by:
I have a complicated Region object, which I need to draw the outline, but not fill How can I convert the Region object to GraphicsPath object? or How can I draw the outline of the Region object?
1
by: Fritz Switzer | last post by:
With two controls a PictureBox and another "PictureBox like" , Pic2, a control from a dll, I'm stuck on cannot implicity convert System.Drawing.Image to System.Drawing.Bitmap error. How do I...
4
by: John | last post by:
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...
7
by: Scott Schluer | last post by:
Is there a way to use the Image class to convert a color photo (GIF or JPEG) to a B&W photo? Thanks, Scott
2
by: Russ Green | last post by:
I'm currentlyworking on a VB.NET application to control Sketchup (http://www.sketchup.com) using COM. Sketchup has a color class with color.red, color.green and color.blue but this is not...
1
by: Sugan | last post by:
Hi all, I'm creating a custom button control. I need to show a color image when the button control is enabled and a gray shade of the same image when the button control is disabled. How can i...
4
by: Jonathan Wood | last post by:
Does anyone know why the toBase argument in Convert.ToString() is limited to 2, 8, 10, or 16? It takes virtually the same code to support all base values from 2 to 36. And can anyone tell me if...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.