Exemple #1
0
        private void Load(Stream str)
        {
            //read the file header and check
            ICONDIR fileheader = new ICONDIR(str);

            if (fileheader.idType != 1)
            {
                throw new Exception("this is not an icon file");
            }
            if (fileheader.idCount < 1)
            {
                throw new Exception("no iconimages contained");
            }
            //read direntries
            ICONDIRENTRY[] entries = new ICONDIRENTRY[fileheader.idCount];
            for (int i = 0; i < fileheader.idCount; i++)
            {
                entries[i] = new ICONDIRENTRY(str);
            }
            //read images
            for (int i = 0; i < fileheader.idCount; i++)
            {
                _images.Add(IconImage.FromStream(str));
            }
        }
Exemple #2
0
        /// <summary>
        /// saves the icon to a stream
        /// </summary>
        public unsafe void Save(Stream str)
        {
            if (str == null)
            {
                throw new ArgumentNullException("str");
            }
            if (_images.Count < 1)
            {
                throw new Exception("icon is empty");
            }
            //write file header
            ICONDIR fileheader = new ICONDIR((short)(_images.Count));

            fileheader.Write(str);
            //write direntries
            int fileoffset = sizeof(ICONDIR) +
                             _images.Count * sizeof(ICONDIRENTRY);

            foreach (IconImage img in _images)
            {
                ICONDIRENTRY entry = img.GetEntry();
                entry.FileOffset = fileoffset;
                fileoffset      += entry.SizeInBytes;
                entry.Write(str);
            }
            //write images
            foreach (IconImage img in _images)
            {
                img.Write(str);
            }
        }
            /// <summary>
            /// constructs an icon from a dll resource
            /// </summary>
            private static Icon IconFromLibrary(IntPtr hlibrary, object resourceid)
            {
                IntPtr hicon;

                //is_intresource
                if (resourceid is int)
                {
                    hicon = Kernel32.FindResource(hlibrary, (int)resourceid, Kernel32.RT_GROUP_ICON);
                }
                else if (resourceid is string)
                {
                    hicon = Kernel32.FindResource(hlibrary, (string)resourceid, Kernel32.RT_GROUP_ICON);
                }
                else
                {
                    throw new ArgumentException("resourceid is invalid type", "resourceid");
                }
                //open stream
                MEMICONDIRENTRY[] entries;
                using (Stream str = Kernel32.GetStreamFromResource(hlibrary, hicon))
                {
                    ICONDIR header = new ICONDIR(str);
                    if (header.idType != 1)
                    {
                        throw new Exception("this is not an icon file");
                    }
                    if (header.idCount < 1)
                    {
                        throw new Exception("no iconimages contained");
                    }
                    //read headers
                    entries = new MEMICONDIRENTRY[header.idCount];
                    for (int i = 0; i < entries.Length; i++)
                    {
                        entries[i] = new MEMICONDIRENTRY(str);
                    }
                }
                Icon ret = new Icon();

                //read images
                for (int i = 0; i < entries.Length; i++)
                {
                    //stream for single image
                    using (Stream str = Kernel32.GetStreamFromResource(hlibrary,
                                                                       Kernel32.FindResource(hlibrary, entries[i].ID, Kernel32.RT_ICON)))
                    {
                        ret.Images.Add(IconImage.FromStream(str));
                    }
                }
                return(ret);
            }