Access pixel colors from an image file

For everyone, just starting with C++ or programming at all. Ask newbie questions in this forum!

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Access pixel colors from an image file

Postby blitzkrieg64 » Fri Jun 22, 2012 3:30 pm

I'm trying to access the pixel colors from a image file but can't seem to get it to work properly, my program is only accessing the pixel colors from my active window. I've been stuck on this for a few days and would really appreciate any help. I'm guessing i haven't set a proper handle to a device context to the image but i don't know how to do this

Code: Select all
HWND hDesktopWnd; HDC hDesktopDC; HDC hCaptureDC;
HBITMAP hCaptureBitmap;
BITMAPINFO bmi = {0};
RGBQUAD *pPixels;
LPTSTR szFileName;
BITMAP  bm;  int nScreenWidth, nScreenHeight;
szFileName = "test.bmp";
     

          nScreenWidth =  GetSystemMetrics(SM_CXSCREEN);
         nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
         hDesktopWnd = GetDesktopWindow();
         hDesktopDC = GetDC(hwnd);
         hCaptureDC = CreateCompatibleDC(hDesktopDC);
         hCaptureBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
         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);

         
         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);





blitzkrieg64
 
Posts: 1
Joined: Fri Jun 22, 2012 3:27 pm

Re: Access pixel colors from an image file

Postby exomo » Sun Jun 24, 2012 2:41 pm

I'm not sure how exactly you should do it, but I think you don't have to load a file if you want to have the desktop.
When calling GetDC(hwnd) you should pass the hDesktopWnd instead. hwnd is the handle of your window, so you only get the image of this window.
Here is a link to an msdn article: http://msdn.microsoft.com/en-us/library/dd183402 I guess you can use some of their code.
Who needs a signature anyway.
User avatar
exomo
 
Posts: 881
Joined: Fri Sep 26, 2003 12:30 pm
Location: germany->baden


Return to For Beginners

Who is online

Users browsing this forum: Google [Bot] and 2 guests

cron