PC Review


Reply
Thread Tools Rate Thread

convert Uint32 to System.drawing.color

 
 
Yaniv
Guest
Posts: n/a
 
      21st Feb 2007
Hi

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

Thanks in advanced
Yaniv

 
Reply With Quote
 
 
 
 
Yaniv
Guest
Posts: n/a
 
      21st Feb 2007
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


 
Reply With Quote
 
Yaniv
Guest
Posts: n/a
 
      21st Feb 2007
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


 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      21st Feb 2007
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
 
Reply With Quote
 
Armin Zingler
Guest
Posts: n/a
 
      22nd Feb 2007
"Göran Andersson" <(E-Mail Removed)> schrieb
> Armin Zingler wrote:
> > "Göran Andersson" <(E-Mail Removed)> schrieb
> > > > 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

 
Reply With Quote
 
ImageAnalyst
Guest
Posts: n/a
 
      24th Feb 2007
On Feb 21, 5:00 am, "Yaniv" <elgar...@gmail.com> wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Font.Color to System.Drawing.Color in C# Andrew HUANG Microsoft Excel Programming 0 9th Jan 2008 07:01 PM
convert RGB color to System.Drawing.Color? =?Utf-8?B?UmljaA==?= Microsoft VB .NET 3 28th Apr 2005 12:23 AM
How do I convert from a 'double' valued color to a System.Drawing.Color? Nat Microsoft Excel Programming 11 28th Jun 2004 01:28 AM
Re: convert hex to System.Drawing.Color Ken Cox [Microsoft MVP] Microsoft ASP .NET 0 27th Jun 2003 01:53 AM
convert hex to System.Drawing.Color Sreejumon[MVP] Microsoft ASP .NET 0 27th Jun 2003 01:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:22 AM.