-
Cropping a BitMap.
I am writting a program and need help.
My program is suppose to take a screen shot and make it a bitmap, call it oldmap. I than would like to crop oldmap from its center with a size of 200 by 200 or 50 by 50 or whatever the user would like, and call it newmap.
I would than like to delete the oldmap and place it with the new map. I am confused on how to implement it.
Here is my code.
Code:
//////////////
bool Capture::screenshotGDI(Screenshot &screeny)
{
HWND hwnd = FindWindowA(0, WindowName);
if (hwnd == NULL)
{
cout << "ERROR: HWND not found!" << endl;
return false;
}
int sWidth = GetSystemMetrics(SM_CXSCREEN);
int sHeight = GetSystemMetrics(SM_CYSCREEN);
HDC hdc = GetDC(0);
HDC captureDC = CreateCompatibleDC(hdc);
HBITMAP hBmp = CreateCompatibleBitmap(hdc, sWidth, sHeight);
HGDIOBJ hOld = SelectObject(captureDC, hBmp);
if (!BitBlt(captureDC, 0, 0, sWidth, sHeight, hdc, 0, 0, SRCCOPY | CAPTUREBLT))
{
cout << "ERROR: bit-block transfer failed!" << endl;
release(hwnd, hdc, captureDC, hBmp);
return false;
}
SelectObject(captureDC, hBmp);
BITMAPINFO bmpInfo = { 0 };
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
if (!GetDIBits(hdc, hBmp, 0, 0, NULL, &bmpInfo, DIB_RGB_COLORS)) //get bmpInfo
{
cout << "ERROR: Failed to get Bitmap Info." << endl;
release(hwnd, hdc, captureDC, hBmp);
return false;
}
bmpInfo.bmiHeader.biCompression = BI_RGB;
screeny.FreeMemory();
int pixNo = bmpInfo.bmiHeader.biWidth * bmpInfo.bmiHeader.biHeight;
screeny.pixels = new RGBQUAD[pixNo];
if (!screeny.pixels)
{
cout << "ERROR: Failed allocating RGBQUAD[" << pixNo << "]" << endl;
release(hwnd, hdc, captureDC, hBmp);
return false;
}
if (!GetDIBits(hdc, hBmp, 0, bmpInfo.bmiHeader.biHeight, (LPVOID)screeny.pixels, &bmpInfo, DIB_RGB_COLORS))
{
cout << "ERROR: Getting the bitmap buffer!" << endl;
screeny.FreeMemory();
release(hwnd, hdc, captureDC, hBmp);
return false;
}
screeny.width = bmpInfo.bmiHeader.biWidth;
screeny.height = bmpInfo.bmiHeader.biHeight;
screeny.length = pixNo;
release(hwnd, hdc, captureDC, hBmp);
return true;
}
//////////////////
Any help on implementing what was stated would be amazing.
Thank you in advance.
-
Re: Cropping a BitMap.
When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
[Thread moved from non-visual c++ issues as relating to use of Windows APIs.]
-
Re: Cropping a BitMap.
BitBlt(targetDC, x, y, width, height, sourceDC, ...) is what they call cropping. Start thinking from this point.