CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Cropping a BitMap.

Threaded View

  1. #1
    Join Date
    Oct 2016

    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.

    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;
    	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;
    		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.
    Last edited by 2kaud; October 6th, 2016 at 11:49 AM. Reason: Code tags added

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)