Trouble with GetDIBits()
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Trouble with GetDIBits()

  1. #1
    Join Date
    May 2013
    Posts
    3

    Trouble with GetDIBits()

    Hello guys, I'm new to C++ and I couldn't figure out the proper way to store pixel data of a device context into an array using GetDIBits(). Currently I am trying to get the RGB values of a single pixel of a bitmap file:

    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    int main() {HDC MemDC=CreateCompatibleDC(NULL);
    	SelectObject(MemDC,(HBITMAP)LoadImage(NULL,(LPCTSTR)"F:\\gBit.bmp",IMAGE_BITMAP,1366,768,LR_LOADFROMFILE));
    	HBITMAP hBit=(HBITMAP)LoadImage(NULL,(LPCTSTR)"F:\\gBit.bmp",IMAGE_BITMAP,1366,768,LR_LOADFROMFILE);
    	BITMAPINFO bmi;
    	BYTE p[3];
    
    
    	bmi.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    	bmi.bmiHeader.biWidth=1;
    	bmi.bmiHeader.biHeight=-1;
    	bmi.bmiHeader.biPlanes=1;
    	bmi.bmiHeader.biBitCount=24;
    	bmi.bmiHeader.biCompression=BI_RGB;
    	bmi.bmiHeader.biSizeImage=0;
    	bmi.bmiHeader.biXPelsPerMeter=0;
    	bmi.bmiHeader.biYPelsPerMeter=0;
    	bmi.bmiHeader.biClrUsed=0;
    	bmi.bmiHeader.biClrImportant=0;
    
    
    	GetDIBits(MemDC,hBit,0,0,p,&bmi,DIB_RGB_COLORS);
    	cout<<p[0]<<endl<<p[1]<<endl<<p[2];
    	ReleaseDC(NULL,MemDC); DeleteDC(MemDC);
    	DeleteObject(hBit); while (1) {}
    }
    gBit.bmp, the bitmap file selected into MemDC, is an entirely white 24-bit bitmap image, so I thought cout should display 255, but for some reason it displays some weird characters. Most probably p is not initialized since the same values are returned when I remove the line containing GetDIBits(). Am I using this function properly? What could I possibly be doing wrong here?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,378

    Re: Trouble with GetDIBits()

    What does the GetDIBits return? And did you red the GetDIBits documentation?
    Victor Nijegorodov

  3. #3
    Join Date
    May 2013
    Posts
    3

    Re: Trouble with GetDIBits()

    Well, in my case the return value is 1, so I guess the pixel is successfully scanned. But whether or not I call GetDIBits(), the same characters are stored in the array. I did read the documentation of the function but I didn't find anything that could cause the error. What do you expect me to know from the documentation?

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,877

    Re: Trouble with GetDIBits()

    BYTE is a char.
    cout << (BYTE)32;
    outputs a character space (ascii value 32), not the value 32.

    to display char's (BYTE's) as numeric values, typecast to int.

  5. #5
    Join Date
    May 2013
    Posts
    18

    Re: Trouble with GetDIBits()

    First, in your other thread COLORREF is 4-bytes, as it stores 32-bit values RGBA (RGB and Alpha for transparency). Both your examples declare 24-bit colour values, so you would need only 3-bytes per pixel. It's ok to use 4-bytes like COLORREF, but you would ignore the 4th byte each time as it would be 0. Though, that is a waste of memory.

    In this example you are loading an image the size 1366x768. So you would need (1366 * 768 * 3) bytes to store it 3147264. Fortunately the width (768) is a multiple of 4, otherwise you would need to add some padding.

    Code:
    BYTE p[3];
    Should be more like
    Code:
    BYTE p[1366 * 768 * 3]  // Can't actually be done
    This would need to be done with new in the heap otherwise it will crash.

    Code:
    bmi.bmiHeader.biSizeImage=0;
    This also needs to be the value of 1366 * 768 * 3.

  6. #6
    Join Date
    May 2013
    Posts
    18

    Re: Trouble with GetDIBits()

    Also, the fourth variable needs to be the image height (scanlines). So instead of:
    Code:
    GetDIBits(MemDC,hBit,0,0,p,&bmi,DIB_RGB_COLORS); // Incorrect
    It should be more like:
    Code:
    GetDIBits(MemDC,hBit,0,768,p,&bmi,DIB_RGB_COLORS);

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center