private static void Add(Bitmap bm, ImageList il)
{
IntPtr hBitmap, ppvBits;
BITMAPINFO bmi = new BITMAPINFO();
if (bm.Size != il.ImageSize) { // resize the image to dimensions of imagelist before adding
bm = new Bitmap(bm, il.ImageSize.Width, il.ImageSize.Height);
}
bmi.biSize = 40; // Needed for RtlMoveMemory()
bmi.biBitCount = 32; // Number of bits
bmi.biPlanes = 1; // Number of planes
bmi.biWidth = bm.Width; // Width of our new bitmap
bmi.biHeight = bm.Height; // Height of our new bitmap
bm.RotateFlip(RotateFlipType.RotateNoneFlipY); // Required due to the way bitmap is copied
hBitmap = CreateDIBSection(new IntPtr(0), bmi, 0, out ppvBits, new IntPtr(0), 0);
BitmapData bitmapData = bm.LockBits(new Rectangle(0, 0, bm.Width, bm.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
RtlMoveMemory(ppvBits, bitmapData.Scan0, bm.Height * bitmapData.Stride); // copies the bitmap
bm.UnlockBits(bitmapData);
ImageList_Add(il.Handle, hBitmap, new IntPtr(0)); // adds the new bitmap to the imagelist control
}