[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 :eek:. How would I do it in C#, but convert them to their original values as bytes?
Any snippets would be greatly appreciated :D
chem
Re: New Thread, New Question :)
Code:
byte b = (byte) shortVal;
I hope this is what you are asking for
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.
Re: New Thread, New Question :)
Quote:
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 :p
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 :D
EDIT: Bah, I gave you reputation recently jmc. Stupid forums. Thanks again :)
chem
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.
Re: [RESOLVED] New Thread, New Question :)
Quote:
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. ;) :)
Re: [RESOLVED] New Thread, New Question :)
Its a better plan than using byte arrays.