Getting Pixels Help

Hello, recently I've been trying to read a lot of pixels quickly; GetPixel() is way too slow so I had a look around and found BitBlt which should be much faster for what I want to do. I couldn't find any working examples of BitBlt so I've put together some code which is made up of the non-working examples and here's what I've got:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;

int main()
{


HDC hScreenDC = GetDC(0);
HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

int x = GetDeviceCaps(hScreenDC, HORZRES);
int y = GetDeviceCaps(hScreenDC, VERTRES);

HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);

HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);


RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect(hDesktop, &desktop);
int horizontal = desktop.right;
int vertical = desktop.bottom;


BitBlt(hMemoryDC, 0, 0, horizontal, vertical, hScreenDC, 0, 0, SRCCOPY);
hBitmap = (HBITMAP)SelectObject(hMemoryDC, hOldBitmap);

BITMAPINFOHEADER   bi;

bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = horizontal;
bi.biHeight = vertical;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
COLORREF Colour;

DWORD dwBmpSize = ((horizontal * bi.biBitCount + 31) / 32) * 4 * vertical;
HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
BYTE *lpbitmap = new BYTE[dwBmpSize];



//BITMAP bm;
GetDIBits(hMemoryDC,hBitmap,0,(UINT)vertical,lpbitmap, (BITMAPINFO *)&bi, DIB_RGB_COLORS);
GetObject (hBitmap, sizeof(lpbitmap), &lpbitmap);


unsigned long red = 0, green = 0, blue = 0;

    for (int i=0; i<(horizontal * 4 * vertical); i+=4)
    {
        red = (int)lpbitmap[i];
        green = (int)lpbitmap[i+1];
        blue = (int)lpbitmap[i+2];
        x = i / (4 * horizontal);
        y = i / (4 * vertical);
        Colour = GetPixel(GetDC(0), x, y);
        cout << int(GetRValue(Colour)) << ", " << int(GetGValue(Colour)) << ", " << int(GetBValue(Colour)) << endl;
        cout << red 
        ReleaseDC(NULL, hScreenDC);
        ReleaseDC(NULL, hMemoryDC);
        DeleteDC(hScreenDC);
        DeleteDC(hMemoryDC);
        DeleteObject(hBitmap);
    }
    return 0;
}


I added in the GetPixel() function to check that I was getting the correct values, but they are not the same when I run the program. Why is this? and am I deleting/cleaning up the variables properly?
Thankyou in advance.
If you have a handle to RGB values (DWORDs, that is, not the 3-byte pixels), you can access the pixel (x, y) with buffer[(y*width)+x]. However, my experience has been with a dib section created by CreateDIBSection
Topic archived. No new replies allowed.