I’m porting a C++ SDK from a USB device to use in C# through pinvoke and and I’m having good progress so far, and the problem now is with a function that builds an image list that I can use to attach to other controls using functions like SetImageList.
What I would like to do is save all the images from this list to the disk and use them later.
There are other functions that returns the index of a paraticular image inside this list, so I could pass this index and then save the image to the disk.
With the
CImageListclass instance (which you get by calling to theDetachmethod on theGetImageListmethod on theCListCtrlinstance that you have), you can get theHIMAGELISThandle by calling the Detach method.From there you can call the underlying Windows API through the Platform Invocation Services (P/Invoke) layer.
The following assumes you have this
HIMAGELISTpointer in anIntPtr:First, you’ll need to declare the
ImageList_GetImageCountfunction in .NET:And of course, store the result of that call in a variable:
You’ll also need to be able to get the details about each image, so you’ll need the call to
ImageList_GetImageInfo:With these, you can start your loop to get the information about the image:
Notice that in the
IMAGEINFOstructure has a field that contains the pointer to the bitmap (thehbmImagefield).This is a handle to an
HBITMAPhandle, which has an equivalent in .NET in theBitmapclass. You can translate between the two by calling the staticFromHbitmapmethod on theImageclass (yes, it’s strange it’s not on theBitmapclass):Once you have the
Bitmapinstance, you can call theSavemethod on each instance to dump them to disk:Note that there is the
ImageListclass in .NET, but there’s no way to pass theHIMAGELISThandle to the managed implementation (which would have made this much easier).