示例#1
0
        void Save()
        {
            if (!_sfd.ShowDialog().Value)
                return;

            try
            {
                using (var nico = new Ico())
                {
                    foreach (var item in IconFrames)
                        nico.Add(item.Bitmap, item.Size, item.KeepAspectRatio);

                    nico.Save(_sfd.FileName);
                }
            }
            catch { }
        }
示例#2
0
        public static Ico Extract(string FileName, int Index)
        {
            var module = LoadLibrary(FileName);

            try
            {
                // Negative Index stands for Resource ID.
                var resourceData = LoadResource(module, Index < 0 ? Kernel32.FindResource(module, new IntPtr(-Index), ResourceType.GroupIcon)
                                                                  : FindResource(module, Index, ResourceType.GroupIcon));

                // Convert the resouce into an .ico file image.
                using (var resourceReader = new BinaryReader(new MemoryStream(resourceData)))
                {
                    // Read the GroupIconDir header.
                    resourceReader.ReadInt16(); // Reserved
                    resourceReader.ReadInt16(); // icon
                    int numEntries = resourceReader.ReadInt16();

                    var icons = new Ico();

                    for (var i = 0; i < numEntries; i++)
                    {
                        using (var icoStream = new MemoryStream())
                            using (var icoWriter = new BinaryWriter(icoStream))
                        {
                            // Write the IconDir header.
                            icoWriter.Write((short)0); // Reserved
                            icoWriter.Write((short)1); // icon
                            icoWriter.Write((short)1); // make single image icon

                            // Read the GroupIconDirEntry.
                            var grpEntry = resourceReader.Read<IconEntry>();
                            var iconId = resourceReader.ReadInt16();

                            // Write the IconDirEntry.
                            grpEntry.Write(icoStream);

                            icoWriter.Write(22); // offset

                            // Get the icon image raw data and write it to the stream.
                            var imgBuf = LoadResource(module, Kernel32.FindResource(module, (IntPtr)iconId, ResourceType.Icon));

                            icoWriter.Write(imgBuf);

                            icoStream.Seek(0, SeekOrigin.Begin);

                            try
                            {
                                icons.Add(new Icon(icoStream).ToBitmap(), grpEntry.Width);
                            }
                            catch (ArgumentOutOfRangeException)
                            {
                                icons.Add(new Bitmap(new MemoryStream(imgBuf)), grpEntry.Width == 0 ? 256 : grpEntry.Width);
                            }
                        }
                    }

                    return icons;
                }
            }
            finally { Kernel32.FreeLibrary(module); }
        }