public override IntPtr Read(IntPtr hModule, IntPtr lpRes)
        {
            _header = (Kernel32.GRPICONDIRENTRY)Marshal.PtrToStructure(
                lpRes, typeof(Kernel32.GRPICONDIRENTRY));

            IntPtr hIconInfo = Kernel32.FindResource(hModule, (IntPtr)_header.nID, (IntPtr)Kernel32.ResourceTypes.RT_ICON);

            if (hIconInfo == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            IntPtr hIconRes = Kernel32.LoadResource(hModule, hIconInfo);

            if (hIconRes == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            IntPtr dibBits = Kernel32.LockResource(hIconRes);

            if (dibBits == IntPtr.Zero)
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            _image.Read(dibBits, (uint)Kernel32.SizeofResource(hModule, hIconRes));

            return(new IntPtr(lpRes.ToInt32() + Marshal.SizeOf(_header)));
        }
        /// <summary>
        /// Convert into an icon resource that can be written into a .dll or .exe.
        /// </summary>
        public IconResource ConvertToIconResource(UInt16 id)
        {
            IconResource iconResource = new IconResource();

            Kernel32.GRPICONDIRENTRY header = new Kernel32.GRPICONDIRENTRY();
            header.bColors       = _header.bColors;
            header.bHeight       = _header.bHeight;
            header.bReserved     = _header.bReserved;
            header.bWidth        = _header.bWidth;
            header.dwImageSize   = _header.dwImageSize;
            header.wBitsPerPixel = _header.wBitsPerPixel;
            header.wPlanes       = _header.wPlanes;
            header.nID           = id;
            iconResource.Header  = header;
            iconResource.Image   = _image;
            return(iconResource);
        }
 /// <summary>
 /// Convert into an icon resource that can be written into a .dll or .exe.
 /// </summary>
 public IconResource ConvertToIconResource(UInt16 id)
 {
     IconResource iconResource = new IconResource();
     Kernel32.GRPICONDIRENTRY header = new Kernel32.GRPICONDIRENTRY();
     header.bColors = _header.bColors;
     header.bHeight = _header.bHeight;
     header.bReserved = _header.bReserved;
     header.bWidth = _header.bWidth;
     header.dwImageSize = _header.dwImageSize;
     header.wBitsPerPixel = _header.wBitsPerPixel;
     header.wPlanes = _header.wPlanes;
     header.nID = id;
     iconResource.Header = header;
     iconResource.Image = _image;
     return iconResource;
 }
        public override IntPtr Read(IntPtr hModule, IntPtr lpRes)
        {
            _header = (Kernel32.GRPICONDIRENTRY)Marshal.PtrToStructure(
                lpRes, typeof(Kernel32.GRPICONDIRENTRY));

            IntPtr hIconInfo = Kernel32.FindResource(hModule, (IntPtr) _header.nID, (IntPtr)Kernel32.ResourceTypes.RT_ICON);
            if (hIconInfo == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            IntPtr hIconRes = Kernel32.LoadResource(hModule, hIconInfo);
            if (hIconRes == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            IntPtr dibBits = Kernel32.LockResource(hIconRes);
            if (dibBits == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            _image.Read(dibBits, (uint) Kernel32.SizeofResource(hModule, hIconRes));

            return new IntPtr(lpRes.ToInt32() + Marshal.SizeOf(_header));
        }