convert Uint32 to System.drawing.color

Y

Yaniv

Hi

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

Thanks in advanced
Yaniv
 
Y

Yaniv

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 כתב:
 
Y

Yaniv

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 כתב:
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Yaniv said:
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.
 
A

Armin Zingler

Göran Andersson said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top