Exemple #1
0
        public static IconFile LoadFromFile(string filename)
        {
            var instance = new IconFile();

            var bytes       = File.ReadAllBytes(filename);
            var pinnedBytes = GCHandle.Alloc(bytes, GCHandleType.Pinned);

            instance.IconDir   = Marshal.PtrToStructure <ICONDIR>(pinnedBytes.AddrOfPinnedObject());
            instance.IconEntry = new ICONDIRENTRY[instance.IconDir.idCount];
            instance.IconImage = new byte[instance.IconDir.idCount][];

            var offset           = Marshal.SizeOf <ICONDIR>();
            var icondirentrySize = Marshal.SizeOf <ICONDIRENTRY>();

            for (int i = 0; i < instance.IconDir.idCount; i++)
            {
                var entry = Marshal.PtrToStructure <ICONDIRENTRY>(new IntPtr(pinnedBytes.AddrOfPinnedObject().ToInt64() + offset));
                instance.IconEntry[i] = entry;
                instance.IconImage[i] = new byte[entry.dwBytesInRes];
                Buffer.BlockCopy(bytes, (int)entry.dwImageOffset, instance.IconImage[i], 0, (int)entry.dwBytesInRes);

                offset += icondirentrySize;
            }

            pinnedBytes.Free();

            return(instance);
        }
Exemple #2
0
        private static void SetIcon(string pe, string ico)
        {
            var icon = IconFile.LoadFromFile(ico);

            Console.WriteLine($"Loaded icon with {icon.GetImageCount()} image(s).");

            var handle = BeginUpdateResource(pe, false);

            if (handle == IntPtr.Zero)
            {
                Console.WriteLine("Error: could not begin update: " + Marshal.GetLastWin32Error());
                return;
            }

            var iconData = icon.CreateIconGroupData(ICON_BASE_ID);

            foreach (var lang in IconGroupLanguages)
            {
                Console.WriteLine($"Updating for language {lang}...");

                bool res;
                if (IconGroupName != null)
                {
                    res = UpdateResource(handle, new IntPtr((int)ResourceType.RT_GROUP_ICON), IconGroupName, lang, iconData, (uint)iconData.Length);
                }
                else
                {
                    if (IconGroupId == IntPtr.Zero)
                    {
                        IconGroupId = new IntPtr(1);
                    }
                    res = UpdateResource(handle, new IntPtr((int)ResourceType.RT_GROUP_ICON), IconGroupId, lang, iconData, (uint)iconData.Length);
                }

                if (!res)
                {
                    Console.Write("Warning: could not update group icon: " + Marshal.GetLastWin32Error());
                }
                for (int i = 0; i < icon.GetImageCount(); i++)
                {
                    var image = icon.GetImageData(i);
                    res = UpdateResource(handle, new IntPtr((int)ResourceType.RT_ICON), new IntPtr(ICON_BASE_ID + i), lang, image, (uint)image.Length);
                    if (!res)
                    {
                        Console.Write($"Warning: could not update image {i}: {Marshal.GetLastWin32Error()}");
                    }
                }
            }

            EndUpdateResource(handle, false);
            Console.WriteLine("Done.");
        }