The main reason was that the form was initialized a lot many times than we had expected, and the ImageList was generated in the form's InitializeComponent() method. We just made that static, and shared that same ImageList object in all the initialized instances of that form. This controlled the number of GDI instances being created. We didn't had to call DeleteObject() on images either.
Initially, we tried this but, THIS WAS NOT THE REAL SOLUTION, which we figured out later
The modification shown below seems to put check on both, Bitmap handles as well as DC(DeviceContext) handles, and solves the problem in my scenario. I made a call to the WinAPI function DeleteObject() for each one of the image in the ImageList instance inside the form's Dispose() method, which after modification now looks like as shown below :
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
protected override void Dispose(bool disposing)
{
if (disposing)
{
listViewMapping.Clear();
listBoxPropertiesA.Items.Clear();
listBoxPropertiesB.Items.Clear();
if (imageList1 != null)
{
foreach (var img in imageList1.Images)
{
DeleteObject(((System.Drawing.Bitmap)img).GetHbitmap());
}
imageList1.Dispose();
imageList1 = null;
}
if (components != null)
{
components.Dispose();
//DeleteObject();
}
}
base.Dispose(disposing);
}