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

x
  • 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);
imgCrop.releaseDC();

//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?

THANKS!


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

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.
0
 
bisonguy2000Author Commented:
Thanks for the response. Works well.
0
 
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?
0

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