示例#1
0
        public void Remove(IconImage image)
        {
            Source.Remove(image.ResourceData.Lang);

            _images.Remove(image);
        }
示例#2
0
        /// <summary>Saves as an ICO to the specified Stream</summary>
        public void Save(Stream stream)
        {
            BinaryWriter wtr = new BinaryWriter(stream);

            IconDirectory dir = new IconDirectory();

            dir.wReserved = 0;
            dir.wType     = (ushort)(Type == IconType.Icon ? 1u : 2u);
            dir.wCount    = (ushort)_images.Count;

            dir.Write(wtr);

            //////////////////////////////////

            _images.Sort();

            // Calculate offsets

            UInt32 offsetSoFar = (uint)(IconDirectory.Size + _images.Count * FileIconDirectoryEntry.Size);

            UInt32[] offsets = new UInt32[_images.Count];

            for (int i = 0; i < _images.Count; i++)
            {
                offsets[i] = offsetSoFar;

                offsetSoFar += (uint)(_images[i] as IconImage).ImageData.Length;

                if (Type == IconType.Cursor)
                {
                    offsetSoFar -= 4;
                }
            }

            //////////////////////////////////

            for (int i = 0; i < _images.Count; i++)
            {
                IconImage image = _images[i] as IconImage;

                FileIconDirectoryEntry entry = new FileIconDirectoryEntry();
                entry.bWidth  = (byte)image.Size.Width;
                entry.bHeight = (byte)image.Size.Height;

                entry.bColorCount = image.ColorCount;
                entry.bReserved   = 0;

                if (Type == IconType.Icon)
                {
                    entry.wPlanes      = image.Planes;
                    entry.wBitCount    = image.BitCount;
                    entry.dwBytesInRes = (uint)image.ImageData.Length;
                }
                else if (Type == IconType.Cursor)
                {
                    entry.wXHotspot    = (ushort)image.Hotspot.X;
                    entry.wYHotspot    = (ushort)image.Hotspot.Y;
                    entry.dwBytesInRes = (uint)image.ImageData.Length - 4;
                }

                entry.dwImageOffset = offsets[i];

                entry.Write(wtr);
            }

            //////////////////////////////////

            for (int i = 0; i < _images.Count; i++)
            {
                IconImage image = _images[i] as IconImage;

                if (Type == IconType.Icon)
                {
                    wtr.Write(image.ImageData);
                }
                else if (Type == IconType.Cursor)
                {
                    // the first 4 bytes (2 words) of a cursor's image data are discarded

                    wtr.Write(image.ImageData, 4, image.ImageData.Length - 4);
                }
            }
        }