Ejemplo n.º 1
0
        public WORD  ID;             // the ID

        /// <summary>
        /// Converts the current TAFactory.IconPack.GroupIconDirEntry into TAFactory.IconPack.IconDirEntry.
        /// </summary>
        /// <param name="id">The resource identifier.</param>
        /// <returns>TAFactory.IconPack.IconDirEntry</returns>
        public IconDirEntry ToIconDirEntry(int imageOffiset)
        {
            IconDirEntry entry = new IconDirEntry();

            entry.Width       = this.Width;
            entry.Height      = this.Height;
            entry.ColorCount  = this.ColorCount;
            entry.Reserved    = this.Reserved;
            entry.Planes      = this.Planes;
            entry.BitCount    = this.BitCount;
            entry.BytesInRes  = this.BytesInRes;
            entry.ImageOffset = imageOffiset;
            return(entry);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Merges a list of icons into one single icon.
        /// </summary>
        /// <param name="icons">The icons to be merged.</param>
        /// <returns>System.Drawing.Icon that contains all the images of the givin icons.</returns>
        public static Icon Merge(params Icon[] icons)
        {
            List <IconInfo> list      = new List <IconInfo>(icons.Length);
            int             numImages = 0;

            foreach (Icon icon in icons)
            {
                if (icon != null)
                {
                    IconInfo info = new IconInfo(icon);
                    list.Add(info);
                    numImages += info.Images.Count;
                }
            }
            if (list.Count == 0)
            {
                throw new ArgumentNullException("icons", "The icons list should contain at least one icon.");
            }

            //Write the icon to a stream.
            MemoryStream outputStream = new MemoryStream();
            int          imageIndex   = 0;
            int          imageOffset  = IconInfo.SizeOfIconDir + numImages * IconInfo.SizeOfIconDirEntry;

            for (int i = 0; i < list.Count; i++)
            {
                IconInfo iconInfo = list[i];
                //The firs image, we should write the icon header.
                if (i == 0)
                {
                    //Get the IconDir and update image count with the new count.
                    IconDir dir = iconInfo.IconDir;
                    dir.Count = (short)numImages;

                    //Write the IconDir header.
                    outputStream.Seek(0, SeekOrigin.Begin);
                    Utility.WriteStructure <IconDir>(outputStream, dir);
                }
                //For each image in the current icon, we should write the IconDirEntry and the image raw data.
                for (int j = 0; j < iconInfo.Images.Count; j++)
                {
                    //Get the IconDirEntry and update the ImageOffset to the new offset.
                    IconDirEntry entry = iconInfo.IconDirEntries[j];
                    entry.ImageOffset = imageOffset;

                    //Write the IconDirEntry to the stream.
                    outputStream.Seek(IconInfo.SizeOfIconDir + imageIndex * IconInfo.SizeOfIconDirEntry, SeekOrigin.Begin);
                    Utility.WriteStructure <IconDirEntry>(outputStream, entry);

                    //Write the image raw data.
                    outputStream.Seek(imageOffset, SeekOrigin.Begin);
                    outputStream.Write(iconInfo.RawData[j], 0, entry.BytesInRes);

                    //Update the imageIndex and the imageOffset
                    imageIndex++;
                    imageOffset += entry.BytesInRes;
                }
            }

            //Create the icon from the stream.
            outputStream.Seek(0, SeekOrigin.Begin);
            Icon resultIcon = new Icon(outputStream);

            outputStream.Close();

            return(resultIcon);
        }
Ejemplo n.º 3
0
        public WORD  ID;             // the ID

        /// <summary>
        /// Converts the current TAFactory.IconPack.GroupIconDirEntry into TAFactory.IconPack.IconDirEntry.
        /// </summary>
        /// <param name="id">The resource identifier.</param>
        /// <returns>TAFactory.IconPack.IconDirEntry</returns>
        public IconDirEntry ToIconDirEntry(int imageOffiset)
        {
            IconDirEntry entry = new IconDirEntry();
            entry.Width = this.Width;
            entry.Height = this.Height;
            entry.ColorCount = this.ColorCount;
            entry.Reserved = this.Reserved;
            entry.Planes = this.Planes;
            entry.BitCount = this.BitCount;
            entry.BytesInRes = this.BytesInRes;
            entry.ImageOffset = imageOffiset;
            return entry;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Loads the icon information from the givin icon into class members.
        /// </summary>
        /// <param name="icon">A System.Drawing.Icon object to retrieve the information about.</param>
        private void LoadIconInfo(Icon icon)
        {
            if (icon == null)
            {
                throw new ArgumentNullException("icon");
            }

            this.SourceIcon = icon;
            MemoryStream inputStream = new MemoryStream();

            this.SourceIcon.Save(inputStream);

            inputStream.Seek(0, SeekOrigin.Begin);
            IconDir dir = Utility.ReadStructure <IconDir>(inputStream);

            this.IconDir      = dir;
            this.GroupIconDir = dir.ToGroupIconDir();

            this.Images              = new List <Icon>(dir.Count);
            this.IconDirEntries      = new List <IconDirEntry>(dir.Count);
            this.GroupIconDirEntries = new List <GroupIconDirEntry>(dir.Count);
            this.RawData             = new List <byte[]>(dir.Count);

            IconDir newDir = dir;

            newDir.Count = 1;
            for (int i = 0; i < dir.Count; i++)
            {
                inputStream.Seek(SizeOfIconDir + i * SizeOfIconDirEntry, SeekOrigin.Begin);

                IconDirEntry entry = Utility.ReadStructure <IconDirEntry>(inputStream);

                this.IconDirEntries.Add(entry);
                this.GroupIconDirEntries.Add(entry.ToGroupIconDirEntry(i));

                byte[] content = new byte[entry.BytesInRes];
                inputStream.Seek(entry.ImageOffset, SeekOrigin.Begin);
                inputStream.Read(content, 0, content.Length);
                this.RawData.Add(content);

                IconDirEntry newEntry = entry;
                newEntry.ImageOffset = SizeOfIconDir + SizeOfIconDirEntry;

                MemoryStream outputStream = new MemoryStream();
                Utility.WriteStructure <IconDir>(outputStream, newDir);
                Utility.WriteStructure <IconDirEntry>(outputStream, newEntry);
                outputStream.Write(content, 0, content.Length);

                outputStream.Seek(0, SeekOrigin.Begin);
                Icon newIcon = new Icon(outputStream);
                outputStream.Close();

                this.Images.Add(newIcon);
                if (dir.Count == 1)
                {
                    this.BestFitIconIndex = 0;

                    this.Width      = entry.Width;
                    this.Height     = entry.Height;
                    this.ColorCount = entry.ColorCount;
                    this.Planes     = entry.Planes;
                    this.BitCount   = entry.BitCount;
                }
            }
            inputStream.Close();
            this.ResourceRawData = GetIconResourceData();

            if (dir.Count > 1)
            {
                this.BestFitIconIndex = GetBestFitIconIndex();

                this.Width      = this.IconDirEntries[this.BestFitIconIndex].Width;
                this.Height     = this.IconDirEntries[this.BestFitIconIndex].Height;
                this.ColorCount = this.IconDirEntries[this.BestFitIconIndex].ColorCount;
                this.Planes     = this.IconDirEntries[this.BestFitIconIndex].Planes;
                this.BitCount   = this.IconDirEntries[this.BestFitIconIndex].BitCount;
            }
        }