GetDIBits() returns wrong BGR values:
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: GetDIBits() returns wrong BGR values:

  1. #1
    Join Date
    May 2013
    Posts
    3

    GetDIBits() returns wrong BGR values:

    GetDIBits() was not passing the correct BGR values to a COLORREF array:

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main() {int i; HBITMAP hBit; HDC bdc; BITMAPINFO bmpInfo; COLORREF pixel[100];
    
    
        hBit=(HBITMAP)LoadImage(NULL,(LPCTSTR)"F:\\bitmap.bmp",IMAGE_BITMAP,10,10,LR_LOADFROMFILE);
        bdc=CreateCompatibleDC(NULL);
        SelectObject(bdc,hBit);
    
    
        bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFO);
        bmpInfo.bmiHeader.biWidth=10;
        bmpInfo.bmiHeader.biHeight=-10;
        bmpInfo.bmiHeader.biPlanes=1;
        bmpInfo.bmiHeader.biBitCount=24;
        bmpInfo.bmiHeader.biCompression=BI_RGB;
        bmpInfo.bmiHeader.biSizeImage=0;
    
    
        GetDIBits(bdc,hBit,0,10,pixel,&bmpInfo,DIB_RGB_COLORS);
    
    
        for (i=0; i<100; i++) {
            cout<<GetBValue(pixel[i]);
            cout<<GetGValue(pixel[i]);
            cout<<GetRValue(pixel[i]);
            cout<<endl;
        }
    
    
        ReleaseDC(NULL,bdc);
        DeleteDC(bdc);
        DeleteObject(hBit);
        free(pixel);
        while (1) {}
    }
    bitmap.bmp is an entirely blue (RGB(0,0,255)) 10x10 24-bit bitmap file. The first few lines of the output look like:

    0
    0
    255

    255
    0
    0

    0
    255
    0

    0
    0
    255

    And it's not only the order of the values that changes; some color values are 0 when they shouldn't be. The last few COLORREF values are RGB(0,0,0). What could be the problem with the code?

  2. #2
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,643

    Re: GetDIBits() returns wrong BGR values:

    COLORREF[100] is not an array of 24bit RGB values.

    it's an array of COLORREF's
    COLORREF which happen to be defined as a typedef to a DWORD (4bytes)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center