|
-
Oct 16th, 2006, 08:50 PM
#1
Thread Starter
G&G Moderator
[RESOLVED] New Thread, New Question :)
Howdy all,
I've constructed a color value from 3 bytes of R, G and B. This is stored in an int...
What I want to know is, how would I retrieve the values back into bytes again? I have a few VB6 examples but they all turn them into integers (shorts), and I'm not sure how to implement even those . How would I do it in C#, but convert them to their original values as bytes?
Any snippets would be greatly appreciated 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Oct 17th, 2006, 11:31 AM
#2
Re: New Thread, New Question :)
Code:
byte b = (byte) shortVal;
I hope this is what you are asking for
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 17th, 2006, 08:17 PM
#3
Re: New Thread, New Question :)
Code:
byte[] colourBytes = BitConverter.GetBytes(colourInt);
Note that you can also do the following if your integer was created in the correct format in the first place, including an alpha value:
Code:
Color clr = Color.FromArgb(colourInt);
Then you can get the A, R, G and B values from the appropriate properties of the Color object.
-
Oct 17th, 2006, 11:07 PM
#4
Thread Starter
G&G Moderator
Re: New Thread, New Question :)
 Originally Posted by jmcilhinney
Code:
byte[] colourBytes = BitConverter.GetBytes(colourInt);
Note that you can also do the following if your integer was created in the correct format in the first place, including an alpha value:
Code:
Color clr = Color.FromArgb(colourInt);
Then you can get the A, R, G and B values from the appropriate properties of the Color object.
Woah. If I had known it was that simple, I wouldn't have spent days researching it 
I ended up with this:
PHP Code:
r = (byte)(realVal & 0xFF);
g = (byte)((realVal & 0xFF00FF00) / 0x100);
bc = (byte)((realVal & 0xFF0000) / 0x10000);
The other way seems alot easier. Thanks heaps 
EDIT: Bah, I gave you reputation recently jmc. Stupid forums. Thanks again 
chem
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Oct 18th, 2006, 07:01 AM
#5
Re: [RESOLVED] New Thread, New Question :)
Storing 3 bytes of bitmap data in a 4 byte variable is just a waste of a byte.
I'd be inclined to create a struct...
struct pixel
{
byte B;
byte G;
byte R;
}
and use "unsafe" code (pointers) to manage the mathematics behind it.
I don't live here any more.
-
Oct 18th, 2006, 07:34 AM
#6
Re: [RESOLVED] New Thread, New Question :)
 Originally Posted by wossname
Storing 3 bytes of bitmap data in a 4 byte variable is just a waste of a byte.
I'd be inclined to create a struct...
struct pixel
{
byte B;
byte G;
byte R;
}
and use "unsafe" code (pointers) to manage the mathematics behind it.
Of course you would.
-
Oct 18th, 2006, 12:52 PM
#7
Re: [RESOLVED] New Thread, New Question :)
Its a better plan than using byte arrays.
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|