문제

I can load the Color contents of the Current Window using GetDIBits but i don't know how to load an image's colors from a location. Can somebody tell me how to do it ?

        char str[256];
        HDC hdc; 
        HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
        HBITMAP hCaptureBitmap; BITMAPINFO bmi = {0};
        RGBQUAD *pPixels; 
        int nScreenWidth, nScreenHeight;
        hdc = GetDC(hwnd);
        GetWindowRect(hwnd,&rect);

        hdc = GetDC(hwnd);
        if(GetWindowRect(hwnd, &rect))
        {
        width = rect.right - rect.left;
        height = rect.bottom - rect.top;
        }

            nScreenWidth =  GetSystemMetrics(SM_CXSCREEN);
            nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
            hDesktopWnd = GetDesktopWindow();
            hDesktopDC = GetDC(hwnd);
            hCaptureDC = CreateCompatibleDC(hDesktopDC);
            hCaptureBitmap = CreateCompatibleBitmap(hDesktopDC, nScreenWidth, nScreenHeight);
            SelectObject(hCaptureDC, hCaptureBitmap); 
            BitBlt(hCaptureDC, 0, 0, nScreenWidth, nScreenHeight, hDesktopDC, 0,0, SRCCOPY|CAPTUREBLT); 

            bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
            bmi.bmiHeader.biWidth = nScreenWidth;
            bmi.bmiHeader.biHeight = nScreenHeight;
            bmi.bmiHeader.biPlanes = 1;
            bmi.bmiHeader.biBitCount = 32;
            bmi.bmiHeader.biCompression = BI_RGB;

            pPixels = new RGBQUAD[nScreenWidth * nScreenHeight];

            ::GetDIBits(hCaptureDC,
                        hCaptureBitmap,
                        0,  
                        nScreenHeight,  
                        pPixels, 
                        &bmi,  
                        DIB_RGB_COLORS); 

I load the colors onto an array this way

            for(int i= 0;i< nScreenHeight; i++){
            for(int j= 0;j< nScreenWidth; j++){
                col.red_palette[i][j]   = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbRed;   
                col.green_palette[i][j] = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbGreen; 
                col.blue_palette[i][j]  = pPixels[(nScreenWidth * (nScreenHeight-(i+1))) + j].rgbBlue;  
            }
            }


            delete [] pPixels;
            ReleaseDC(hDesktopWnd, hDesktopDC);
            DeleteDC(hCaptureDC);
            DeleteObject(hCaptureBitmap);

i'm new in windows programming and just want to know how to load an image onto the hdc.

As Raymond has suggested i've passed the second parameter as

HBITMAP hCaptureBitmap;
hCaptureBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
            LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );

I'm still only capturing the active window's colors and not the image's. How do i change the device handle to reflect that.

HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
hDesktopWnd = GetDesktopWindow();
hDesktopDC = GetDC(hwnd);
hCaptureDC = CreateCompatibleDC(hDesktopDC);
도움이 되었습니까?

해결책

RGBQUAD rgba = pPixels[y * nScreenWidth + x];

Note that y=0 is at the bottom of the image, not the top as you might expect.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top