Results 1 to 7 of 7

Thread: [RESOLVED] New Thread, New Question :)

  1. #1

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Resolved [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

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    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

    I ended up with this:
    PHP Code:
    = (byte)(realVal 0xFF);
    = (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

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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
  •  



Click Here to Expand Forum to Full Width