Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / MFC
Article

Extracting Single Images from a CImageList object

Rate me:
Please Sign up or sign in to vote.
4.59/5 (23 votes)
31 Jul 2003Public Domain1 min read 152K   29   30
This will show you how extract an individual CBitmap from a CImageList object

Introduction

While working on a project of mine using skinned buttons, I ran into problems when I tried to use a CImageList as the source of the state images.

The CImageList does not provide any direct access to the separate images within the list. I searched, and searched and searched and search... well you get the idea... Anyway after banging my head against the keyboard for 4 days, I finally found a solution.

The GetImageFromList function

The result of my headaches is the GetImageFromList function. If you are familiar with using CDCs then you are probably already calling this an obvious solution - but then, why are you reading this article in the first place?

Here is the function I managed to come up with (with the help of sites like this):

It takes 3 parameters, and returns nothing.

  • lstImages: A pointer to the CImageList object containing all of the images.
  • nImage: The index of the image that is going to be extracted.
  • destBitmap: A pointer to the CBitmap object that is going to contain the extracted image.

The function makes a copy of the image list, and moves the requested image to the front of that list.

It then draws the requested image into the destination bitmap.

void CMyWindowClass::GetImageFromList(CImageList *lstImages, 
                             int nImage, CBitmap* destBitmap)
{    
     //First we want to create a temporary image list we can manipulate
     CImageList tmpList;
     tmpList.Create(lstImages);
     
    //Then swap the requested image to the first spot in the list 
    tmpList.Copy( 0, nImage, ILCF_SWAP );
    
    //Now we need to get som information about the image 
    IMAGEINFO lastImage;
    tmpList.GetImageInfo(0,&lastImage);
    
    //Heres where it gets fun
    //Create a Compatible Device Context using 
    //the valid DC of your calling window
    CDC dcMem; dcMem.CreateCompatibleDC (GetWindowDC()); 
    
    //This rect simply stored the size of the image we need
    CRect rect (lastImage.rcImage);
    
    //Using the bitmap passed in, Create a bitmap 
    //compatible with the window DC
    //We also know that the bitmap needs to be a certain size.
    destBitmap->CreateCompatibleBitmap (this->GetWindowDC(), 
                                      rect.Width (), rect.Height ());
    
    //Select the new destination bitmap into the DC we created above
    CBitmap* pBmpOld = dcMem.SelectObject (destBitmap);
    
    //This call apparently "draws" the bitmap from the list, 
    //onto the new destination bitmap
    tmpList.DrawIndirect (&dcMem, 0, CPoint (0, 0), 
           CSize (rect.Width (), rect.Height ()), CPoint (0, 0));
    
    
    //cleanup by reselecting the old bitmap object into the DC
    dcMem.SelectObject (pBmpOld);
}

It looks big, but remove the comments and you have a mere 12 lines of code.

Conclusion

I hope that this article can save at least one person from going through what I went through to find the answer.

Good Luck, and Happy Coding!!

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCreate Multipage Images Pin
Game-point30-Sep-09 21:50
Game-point30-Sep-09 21:50 
QuestionIs it really your code? Pin
kifa3329-Mar-08 10:00
kifa3329-Mar-08 10:00 
AnswerRe: Is it really your code? Pin
leonbourassa22-Dec-08 14:40
leonbourassa22-Dec-08 14:40 
GeneralGetImageInfo fails with ILC_COLOR8 Pin
Nyarlatotep29-Oct-07 9:51
Nyarlatotep29-Oct-07 9:51 
GeneralOr if using GDI+ Pin
joe_turpin1-Aug-07 10:23
joe_turpin1-Aug-07 10:23 
NewsMemory EATER !!! Pin
MacGadger24-Sep-06 6:01
MacGadger24-Sep-06 6:01 
GeneralRe: Memory EATER !!! Pin
wangdxhsuperman1-Feb-07 19:06
wangdxhsuperman1-Feb-07 19:06 
QuestionHow to Load from Disk Drive???? Pin
MacGadger25-Jul-06 22:08
MacGadger25-Jul-06 22:08 
AnswerRe: How to Load from Disk Drive???? Pin
David Crow2-Mar-11 5:06
David Crow2-Mar-11 5:06 
GeneralThank you Pin
Fastfootskater30-Aug-05 20:19
Fastfootskater30-Aug-05 20:19 
GeneralCImageList from a set of CBitmap Pin
Divya Rathore23-Apr-05 1:15
Divya Rathore23-Apr-05 1:15 
GeneralRe: CImageList from a set of CBitmap Pin
Divya Rathore23-Apr-05 1:49
Divya Rathore23-Apr-05 1:49 
GeneralThank you! Pin
Michael Klim15-Feb-05 9:05
Michael Klim15-Feb-05 9:05 
QuestionWhy not just use CopyImage()? Pin
spirit2kxx9-Feb-05 12:28
spirit2kxx9-Feb-05 12:28 
AnswerRe: Why not just use CopyImage()? Pin
synphreak24-Jun-05 10:11
synphreak24-Jun-05 10:11 
GeneralRe: Why not just use CopyImage()? Pin
LuisM22-Aug-05 2:15
professionalLuisM22-Aug-05 2:15 
GeneralI am puzzled! Pin
CPAVG4-Apr-04 22:13
CPAVG4-Apr-04 22:13 
QuestionWhy this is necessary?? Pin
CPAVG4-Apr-04 22:04
CPAVG4-Apr-04 22:04 
[quote]
//Then swap the requested image to the first spot in the list
tmpList.Copy( 0, nImage, ILCF_SWAP );
[/quote]

Why swap it to the 1st place? Is it necessary???

To be or not to be!
AnswerRe: Why this is necessary?? Pin
synphreak24-Jun-05 10:17
synphreak24-Jun-05 10:17 
GeneralSpeed Pin
Francois Bedard8-Sep-03 10:59
Francois Bedard8-Sep-03 10:59 
GeneralRe: Speed Pin
CPAVG5-Apr-04 5:06
CPAVG5-Apr-04 5:06 
GeneralNice Work BUT.... Pin
Howard Schmaeu8-Sep-03 2:38
Howard Schmaeu8-Sep-03 2:38 
GeneralRe: Nice Work BUT.... Pin
leonbourassa8-Sep-03 4:30
leonbourassa8-Sep-03 4:30 
GeneralRe: Nice Work BUT.... Pin
Howard Schmaeu8-Sep-03 4:39
Howard Schmaeu8-Sep-03 4:39 
GeneralRe: Nice Work BUT.... Pin
leonbourassa8-Sep-03 4:41
leonbourassa8-Sep-03 4:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.