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
|
void Image::LoadImage(HWND hwndSource, RECT rect)
{
//Free the current memory used by scanlines array
delete[] scanlines; // scanlines is a member char array, which may or may not already be filled
scanlines = NULL;
width = rect.right - rect.left;
height = rect.bottom - rect.top;
//Get DC from window
HDC hdcSource = GetDC(hwndSource);
//Create DC into which to select output bitmap
HDC hdcDest = CreateCompatibleDC(hdcSource);
//Create the bitmap
HBITMAP hbmOutput = CreateCompatibleBitmap(hdcSource,width,height);
//Select bitmap into the compatible device context
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcDest,hbmOutput);
//Block-transfer the image data from screen dc into output dc
BitBlt(hdcDest,0,0,width,height,hdcSource,rect.left,rect.top,SRCCOPY);
//Determine padding for use in allocating new memory
int padding = 0;
while ( (width * 3 + padding) % 4 != 0) padding++;
//Create and fill BITMAPINFO structure to pass to GetDIBits
BITMAPINFO bmi;
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = height;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 24;
bmi.bmiHeader.biCompression = 0;
bmi.bmiHeader.biSizeImage = height * (width * 3 + padding);
bmi.bmiHeader.biXPelsPerMeter = 0;
bmi.bmiHeader.biYPelsPerMeter = 0;
bmi.bmiHeader.biClrUsed = 0;
bmi.bmiHeader.biClrImportant = 0;
//Allocate memory
scanlines = new unsigned char[height*(width*3+padding)];
//Acquire bytes from bitmap
GetDIBits(hdcDest,hbmOutput,0,height,scanlines,&bmi,DIB_RGB_COLORS);
//Free output bitmap
SelectObject(hdcDest,hbmOld);
DeleteObject(hbmOutput);
//Free DCs
ReleaseDC(hwndSource,hdcSource);
DeleteDC(hdcDest);
}
|