Click here to Skip to main content
Licence Public Domain
First Posted 31 Jul 2003
Views 101,514
Bookmarked 28 times

Extracting Single Images from a CImageList object

By | 31 Jul 2003 | Article
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

About the Author

leonbourassa



Canada Canada

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCreate Multipage Images Pinmemberraju_Code21:50 30 Sep '09  
QuestionIs it really your code? Pinmemberrom190410:00 29 Mar '08  
AnswerRe: Is it really your code? Pinmemberleonbourassa14:40 22 Dec '08  
GeneralGetImageInfo fails with ILC_COLOR8 PinmemberNyarlatotep9:51 29 Oct '07  
GeneralOr if using GDI+ Pinmemberjoe_turpin10:23 1 Aug '07  
NewsMemory EATER !!! PinmemberManni Singh6:01 24 Sep '06  
GeneralRe: Memory EATER !!! Pinmemberwangdxhsuperman19:06 1 Feb '07  
QuestionHow to Load from Disk Drive???? PinmemberManni Singh22:08 25 Jul '06  
AnswerRe: How to Load from Disk Drive???? PinmemberDavidCrow5:06 2 Mar '11  
GeneralThank you PinmemberFastfootskater20:19 30 Aug '05  
GeneralCImageList from a set of CBitmap PinmemberDivya Rathore1:15 23 Apr '05  
GeneralRe: CImageList from a set of CBitmap PinmemberDivya Rathore1:49 23 Apr '05  
GeneralThank you! PinmemberMichael Klim9:05 15 Feb '05  
QuestionWhy not just use CopyImage()? Pinmemberspirit2kxx12:28 9 Feb '05  
AnswerRe: Why not just use CopyImage()? Pinmembersynphreak10:11 24 Jun '05  
GeneralRe: Why not just use CopyImage()? PinmemberLuisM2:15 22 Aug '05  
GeneralI am puzzled! PinmemberCPAVG22:13 4 Apr '04  
QuestionWhy this is necessary?? PinmemberCPAVG22:04 4 Apr '04  
AnswerRe: Why this is necessary?? Pinmembersynphreak10:17 24 Jun '05  
GeneralSpeed PinsussFrancois Bedard10:59 8 Sep '03  
GeneralRe: Speed PinmemberCPAVG5:06 5 Apr '04  
GeneralNice Work BUT.... PinmemberHoward Schmaeu2:38 8 Sep '03  
GeneralRe: Nice Work BUT.... Pinmemberleonbourassa4:30 8 Sep '03  
GeneralRe: Nice Work BUT.... PinmemberHoward Schmaeu4:39 8 Sep '03  
GeneralRe: Nice Work BUT.... Pinmemberleonbourassa4:41 8 Sep '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 1 Aug 2003
Article Copyright 2003 by leonbourassa
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid