[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2656
  • Last Modified:

crop CImage bitmap to new Cimage

I have a CImage object that I need to extract a portion of the image to a new CImage.
I have been having trouble doing this, I saw another similar post, but there was not specific code for the solution, I am not an MFC BitBlt expert, so I really need a few lines of example code.
What I am trying to do is the following:

CImage imgOriginal;
CImage imgCrop;

imgOriginal = LoadFromResource(AfxGetInstanceHandle(),BMP_ID);  //THIS WORKS FINE
imgCrop = LoadFromResource(AfxGetInstanceHandle(),BMP_ID);  // I do this just to initialize imgCrop with a valid bitmap, although it is not currently the correct bitmap

HDC hdcCrop = imgCrop.getDC();
imgCrop =  imgOriginal.BitBlt(hdcCrop,50,50,SCRCOPY);

//I need imageCrop to contain a subset or in this case a shifted version of the original image, but I cant get it to work

What should I be doing in my code?


1 Solution
HDC hdcSource = imgOriginal.getDC();
HDC hdcDest = imgCrop.getDC();
::BitBlt(hdcDest, 0, 0, 50, 50, hdcSource, 0, 0, SRCCOPY);

Assuming that imgCrop is CImage with size at least 50*50, this code copies (0, 0)-(50, 50) rectangle from imgOriginal to imgCrop. You can play with BitBlt parameters to copy different source bitmap fragments to different places of destination bitmap.
bisonguy2000Author Commented:
Thanks for the response. Works well.
dasun gunaCommented:
This works fine !!

But if I use a PNG with textual content, cropping result is not with that text content.
Why is that?

Also, how can I apply trim after cropping image?

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now