Thread: GetPixel and getting a screenshot.

  1. #16
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Yes...

    GetDIBits()


    Which language are you using that can access some of the WIN32 API (in a DLL) but not all of it?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  2. #17
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    You do realize that you do not need either GetPixel or SetPixel calls. Both can be accomplished by the use of bit shifting to get or set the appropriate RGB values.

  3. #18
    Registered User
    Join Date
    Mar 2009
    Location
    Deutschland
    Posts
    1

    Arrow how to get pixels

    Code:
    	HDC hdcc = CreateCompatibleDC(hdc);
    //hdc - is your dc which you copy from
    
    	HBITMAP hBm = CreateCompatibleBitmap(hdc, iSrcW, iSrcH);
    //iSrcW, iSrcH - size of you sourse hdc
    	HBITMAP hOldBm = (HBITMAP)SelectObject(hdcc, hBm);
    	BitBlt(hdcc, 0, 0, iSrcW, iSrcH, hdc, iX, iY, SRCCOPY);
    
    // now, get info from copied bitmap
    	BITMAP bm;
    	memset(&bm, 0, sizeof BITMAP);
    	int iErr = GetObject(hBm, sizeof(BITMAP), &bm);
    	iErr = GetLastError();
    	iBPP = bm.bmBitsPixel;
    	DIBSECTION dis;
    	iErr = GetObject(hBm, sizeof(DIBSECTION), &dis);
    
    	int length = (bm.bmWidth * bm.bmHeight) * (bm.bmBitsPixel>>3);
    	if(0 == length)
    		return NULL;// nothing to do
    
    // Ok, bitmap info now
    	BITMAPINFO bi;
    	memset(&bi, 0, sizeof BITMAPINFO);
    	BYTE *pBits = new BYTE[length];
    
    	bi.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
    	bi.bmiHeader.biWidth         = bm.bmWidth;
    	bi.bmiHeader.biHeight        = bm.bmHeight;
    	bi.bmiHeader.biPlanes        = 1;
    	bi.bmiHeader.biBitCount      = bm.bmPlanes * bm.bmBitsPixel;
    	bi.bmiHeader.biCompression   = 0;
    	bi.bmiHeader.biSizeImage     = 0;
    	bi.bmiHeader.biXPelsPerMeter = 0;
    	bi.bmiHeader.biYPelsPerMeter = 0;
    	bi.bmiHeader.biClrUsed       = 0;
    	bi.bmiHeader.biClrImportant  = 0;
    
    	LPBYTE lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, length);
    
    	int iBits = GetDIBits(hdc, hBm, 0, bm.bmHeight, lpBits, &bi, DIB_RGB_COLORS);
    
    //.......  now you got bits of your pixels in lpBits
    // remember, that y-axis begun from bottom of screen
    // and don't forget:
    
    	GlobalFree(lpBits);
    	SelectObject(hdcc, hOldBm);
    	DeleteObject(hBm);
    	DeleteDC(hdcc);
    Good luck!

  4. #19
    Registered User
    Join Date
    Apr 2007
    Posts
    137
    Quote Originally Posted by DeskEx View Post
    Good luck!
    See MSDN code.

  5. #20
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Seriously, forget that SetPixel and GetPixel exist. They are completely useless. Using them is like walking down the road to the store to buy a single paperclip every time you need one, even though you go through about 200+ per day.
    Bitblt on the other hand would be like buying a bag full of them.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed