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

Thread: Cropping a BitMap.

  1. #1
    Join Date
    Oct 2016
    Posts
    1

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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,038

    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.]
    Last edited by 2kaud; October 6th, 2016 at 11:53 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.5.2)

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,607

    Re: Cropping a BitMap.

    BitBlt(targetDC, x, y, width, height, sourceDC, ...) is what they call cropping. Start thinking from this point.
    Best regards,
    Igor

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)