crop CImage bitmap to new Cimage

Posted on 2005-04-26
Last Modified: 2015-03-12
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?


Question by:bisonguy2000
    LVL 48

    Accepted 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.

    Author Comment

    Thanks for the response. Works well.

    Expert Comment

    by:dasun guna
    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?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Suggested Solutions

    Title # Comments Views Activity
    c++ saving to a text file using vs2003 3 64
    Generic progress indicator 6 107
    sum28 challenge 31 82
    NotAlone Challenge 20 65
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now