/// <summary>
 /// Constructs a new collection of device images
 /// </summary>
 /// <param name="icons">Icons to add</param>
 public IconDeviceImageCollection(
    IconDeviceImage[] icons
    )
 {
    foreach (IconDeviceImage icon in icons)
    {
       this.InnerList.Add(icon);
    }
 }
      private void loadFromFile(
         string iconFile)
      {
         loadInitialise();

         // Open the file
         FileStream fs = new FileStream(
            iconFile,
            FileMode.Open,
            FileAccess.Read,
            FileShare.Read);
         BinaryReader br = new BinaryReader(fs);

         try
         {
            // read the header:
            int iconCount = readIconFileHeader(br);
            // read the directory:
            ICONDIRENTRY[] ide = new ICONDIRENTRY[iconCount];
            for (int iconEntry = 0; iconEntry < iconCount; iconEntry++)
            {
               ide[iconEntry] = new ICONDIRENTRY(br);
            }
            IconDeviceImage[] icons = new IconDeviceImage[iconCount];
            // read the actual icons:
            for (int iconEntry = 0; iconEntry < iconCount; iconEntry++)
            {
               fs.Seek(ide[iconEntry].dwImageOffset, SeekOrigin.Begin);
               byte[] b = new byte[ide[iconEntry].dwBytesInRes];
               br.Read(b, 0, ide[iconEntry].dwBytesInRes);
               icons[iconEntry] = new IconDeviceImage(b);
            }
            // Add the icons to the collection:
            this.iconCollection = new IconDeviceImageCollection(icons);
         }
         catch (Exception ex)
         {
            if (ex is SystemException)
            {
               throw ex;
            }
            else
            {
               throw new IconExException("Failed to read icon file.", ex);   
            }
         }
         finally
         {
            br.Close();            
         }

         this.iconFile = iconFile;
      }
 /// <summary>
 /// Add a new icon device image
 /// </summary>
 /// <param name="icon">Icon to add</param>
 public void Add(IconDeviceImage icon)
 {
    foreach (IconDeviceImage iconExisting in this.InnerList)
    {
       if (icon.IconSize.Equals(iconExisting.IconSize) &&
          icon.ColorDepth.Equals(iconExisting.ColorDepth))
       {
          throw new IconExException("An Icon Device Image with the same size and colour depth already exists in this icon");
       }
    }
    this.InnerList.Add(icon);
 }
      // this method is too long, I'm sorry...
      private void loadFromLibrary(
         string libraryFile,
         string resourceName
         )
      {
         string msg = "";
         bool failed = false;
         IntPtr hGlobal = IntPtr.Zero;
         IntPtr hRsrc = IntPtr.Zero;
         IntPtr hLibrary = IntPtr.Zero;
         IntPtr lPtr = IntPtr.Zero;

         try
         {
            hLibrary = LoadLibraryEx(               
               libraryFile,
               IntPtr.Zero,
               LOAD_LIBRARY_AS_DATAFILE);
            if (hLibrary != IntPtr.Zero)
            {
               hRsrc = FindResource(
                  hLibrary,
                  resourceName,
                  (IntPtr)RT_GROUP_ICON);
               if (hRsrc != IntPtr.Zero)
               {
                  hGlobal = LoadResource(hLibrary, hRsrc);
                  if (hGlobal != IntPtr.Zero)
                  {
                     lPtr = LockResource(hGlobal);
                     if (lPtr != IntPtr.Zero)
                     {
                        // now we can read the header:
                        int iconCount = readResourceIconFileHeader(lPtr);
                        // read the directory:
                        MEMICONDIRENTRY[] ide = new MEMICONDIRENTRY[iconCount];
                        int ofs = 6;
                        for (int iconEntry = 0; iconEntry < iconCount;
                         iconEntry++)
                        {
                           ide[iconEntry] = new MEMICONDIRENTRY(lPtr, ofs);
                           //Console.WriteLine(ide[iconEntry].ToString());
                           ofs += 14;                           
                        }                        
                        FreeResource(hGlobal);
                        hGlobal = IntPtr.Zero;

                        // we have the directory, so now can load the icons:
                        //IconDeviceImage[] icons = new
                        // IconDeviceImage[iconCount];
						 ArrayList icons = new ArrayList();
                        // read the icons:
                        for (int iconEntry = 0; iconEntry < iconCount;
                         iconEntry++)
                        {
                           // find the specified icon:
                           string resName = String.Format("#{0:G}", //N0
                            ide[iconEntry].nID);
                           hRsrc = FindResource(
                              hLibrary,
                              resName,
                              (IntPtr)RT_ICON);
                           if (hRsrc == IntPtr.Zero)
                           {
                              msg = String.Format(
                                 "Could not find the component icon resource with id {0}", 
                                 ide[iconEntry].nID);
                              failed = true;
                              break;
                           }
                           else
                           {
                              // load the resource:
                              hGlobal = LoadResource(
                                 hLibrary,
                                 hRsrc);
                              if (hGlobal == IntPtr.Zero)
                              {
                                 msg = String.Format(
                                    "Could not load the component icon resource with id {0}", 
                                    ide[iconEntry].nID);
                                 failed = true;
                                 break;
                              }
                              else
                              {
                                 // check the size:
                                 int resSize = SizeofResource(hLibrary, hRsrc);
                                 if ((resSize > 0) && (resSize ==
                                  ide[iconEntry].dwBytesInRes))
                                 {
                                    // ok
                                    lPtr = LockResource(hGlobal);
                                    byte[] b = new byte[resSize];
                                    Marshal.Copy(lPtr, b, 0, resSize);
									 try
									 {
										 //icons[iconEntry] = new IconDeviceImage(b);
										 icons.Add(new IconDeviceImage(b));
									 }
									 catch{}
                                 }
                                 else
                                 {
                                    msg = String.Format(
                                       "Component icon resource with id {0} is corrupt", 
                                       ide[iconEntry].nID);
                                    failed = true;
                                 }
                              }
                           }
                        }
                        if (!failed)
                        {
                           // Add the icons to the collection:
							IconDeviceImage[] temp = new IconDeviceImage[icons.Count];
							icons.CopyTo(temp);
                           this.iconCollection = new
                            IconDeviceImageCollection(temp);
                        }
                     }
                     else
                     {
                        msg = "Can't lock resource for reading.";
                        failed = true;
                     }
                  }
                  else
                  {
                     msg = "Can't load resource for reading.";
                     failed  = true;
                  }
               }
               else
               {
                  msg = "Can't find resource.";
                  failed  = true;
               }
            }
            else
            {
               msg = "Can't load library.";
               failed  = true;
            }
         }
         catch (Exception ex)
         {
            failed = true;
            msg = ex.Message;
         }
         finally
         {
            // clear up handles:
            if (hGlobal != IntPtr.Zero)
            {
               FreeResource(hGlobal);
            }
            if (hLibrary != IntPtr.Zero)
            {
               FreeLibrary(hLibrary);
            }
            if (failed)
            {
               throw new IconExException(msg);
            }
         }

      }