CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio
    Posts
    20

    Question converting byte data to Drawing.Color

    I'm writing a simple app to open Grand Prix Legends' funky PBF files used to store screenshot bitmaps in 16-bit rgb 565 format where each pixel's color is represented by two bytes.

    SetPixel requires a Color parameter. How do I convert those two bytes to System.Drawing.Color type?

    Thanks,
    Homer

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    Re: converting byte data to Drawing.Color

    Check the Color.FromArgb method.

  3. #3
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio
    Posts
    20

    Re: converting byte data to Drawing.Color

    ToARGB/FromARGB will not accept a two byte color value. RGB 565 uses 5 bits for red, 6 bits for green, and 5 bits for blue. ARGB uses one byte each for alpha, red, green, and blue.

    I am having limited success using the color translator:

    Code:
    pixColor = ColorTranslator.FromWin32(buffer(i + 1) * &H100 Or buffer(i))
    imgbmp.SetPixel(x, y, pixColor)
    Unfortunately the resulting image looks like it came from a monochrome monitor.

    Is there a similar method like ToARGB that accepts a two-byte color value that I don't know about?

  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: converting byte data to Drawing.Color

    Maybe you want something like this.. I didn't test it.



    EXAMPLE :


    Code:
     Dim b As Integer
                b = 65535
                Const RED As Integer = 63488
                Const green As Integer = 2016
                Const blue As Integer = 31
    
                Const MaxRed As Integer = 31
                Const MaxGreen As Integer = 63
                Const MaxBlue As Integer = 31
    
                Dim bred As Integer = (b And RED)
                Dim bgreen As Integer = (b And green)
                Dim bblue As Integer = (b And blue)
    
                bred = bred >> 11 ' ------ > Takes out the 0 
                bgreen = bgreen >> 5 ' ----- > takes out the 0
    
                Dim flBred As Decimal = (bred \ MaxRed)    'Now you got the red proportion in byte
                Dim flbgreen As Decimal = (bgreen \ MaxGreen)
                Dim flbblue As Decimal = (bblue \ MaxBlue)
    
                bred = CInt(flBred * 255)
                bgreen = CInt(flbgreen * 255)
                bblue = CInt(flbblue * 255)
    
                Dim newColor As System.Drawing.Color
    
                newColor = New System.Drawing.Color
    
                newColor.FromArgb(bred, bgreen, bblue)
    Nicolas Bohemier

  5. #5
    Join Date
    Sep 2002
    Location
    Cleveland, Ohio
    Posts
    20

    Re: converting byte data to Drawing.Color

    Thanks, Nicolas. I was stumped on how to separate the individual bits.

    But VB.net won't let me use the C++ extraction operator (>>). Is there a VB equivalent?

  6. #6
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: converting byte data to Drawing.Color

    the >> operator in vb.net is the SHIFT operator


    >> = shift right
    << = shift left

    I'm doing the >> shift thing.. I don't see why you need to extract something..


    If you mean read the data from a file or something then you need to check with stream classes I think.

    But I thought you had your bits in memory
    Nicolas Bohemier

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

Featured