CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Threaded View

  1. #1
    Join Date
    Apr 2008
    Posts
    26

    Problem with GetDIBits function

    I'm not sure where the problem is but my function (FindColor) is not returning the correct result. The goal of this function is to find a color in a window by using FindDIBits to create an array of every pixel, and then loop through the array until the pixel is found. Here is my code inside a simple Console App I was using for testing. I'm using Dev-C++ btw.
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    
    using namespace std;
    
    void FindColor (HWND hWnd, COLORREF Color, POINT * Result)
    {
        HDC hDC = GetDC (hWnd);
        
        RECT WinRECT;
        GetClientRect (hWnd, &WinRECT);
        
        HBITMAP hBMP = CreateCompatibleBitmap (hDC, WinRECT.right, WinRECT.bottom);
        
        BITMAPINFO BMPInfo;
        BMPInfo.bmiHeader.biSize = sizeof (BITMAPINFO);
        BMPInfo.bmiHeader.biWidth = WinRECT.right;
        BMPInfo.bmiHeader.biHeight = -WinRECT.bottom;
        BMPInfo.bmiHeader.biPlanes = 1;
        BMPInfo.bmiHeader.biBitCount = 32;
        BMPInfo.bmiHeader.biCompression = BI_RGB;
        
        RGBQUAD * BitArray;
        BitArray = new RGBQUAD[(WinRECT.right - 1) * (WinRECT.bottom - 1)];
        if (0 != GetDIBits (hDC, hBMP, 0, WinRECT.bottom, &BitArray, &BMPInfo, DIB_RGB_COLORS))
        {
            for (int iCountX; iCountX < WinRECT.right; iCountX++)
            {
                for (int iCountY; iCountY < WinRECT.bottom; iCountY++)
                {
                    if (Color = RGB(BitArray[iCountX + (iCountY * (WinRECT.bottom - 1))].rgbRed, BitArray[iCountX + (iCountY * (WinRECT.bottom - 1))].rgbGreen, BitArray[iCountX + (iCountY * (WinRECT.bottom - 1))].rgbBlue))
                    {
                        Result->x = iCountX;
                        Result->y = iCountY;
                        delete [] BitArray;
                        return;
                    }
                }
            }
        }
        delete [] BitArray;
    }
    
    int main(int argc, char *argv[])
    {
        POINT Location;
        FindColor (0, 0xFFFFFF, &Location);
        cout << Location.x << ',' << Location.y << '\n';
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Does anyone know what I've been doing wrong?
    Last edited by Brownhead; April 13th, 2008 at 12:29 PM.

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