/// <summary> /// Writes the icon group as a .ico file /// </summary> /// <param name="path">path to write to</param> /// <param name="reader">reader that holds the PE image</param> internal void Write(string path, BinaryReader reader) { using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(stream)) { ICON_HEADER header = new ICON_HEADER(); header.idCount = Convert.ToUInt16(Entries.Count); header.idReserved = Group.idReserved; header.idType = Group.idType; writer.Write(RawSerialize(header)); long size = Marshal.SizeOf(typeof(ICON_DIRECTORY_ENTRY)); long headerEnd = Marshal.SizeOf(typeof(ICON_HEADER)) + (size * Entries.Count); long baseImageDataOffset = headerEnd; foreach (IconImage entry in Entries) { ICON_DIRECTORY_ENTRY ent = new ICON_DIRECTORY_ENTRY(); ent.ColorCount = entry.Entry.ColorCount; ent.Height = entry.Entry.Height; ent.Reserved = entry.Entry.Reserved; ent.Width = entry.Entry.Width; ent.BytesInRes = entry.Entry.BytesInRes; ent.BitCount = entry.Entry.BitCount; ent.Planes = entry.Entry.Planes; ent.ImageOffset = Convert.ToUInt32(baseImageDataOffset); baseImageDataOffset += entry.Entry.BytesInRes; writer.Write(RawSerialize(ent)); } foreach (IconImage entry in Entries) { writer.Write(entry.GetImageData(reader, m_Stream, entry.GetResourceAddress(SectionBaseAddress, SectionVirtualAddress))); } } FileInfo info = new FileInfo(path); if (!info.Directory.Exists) { info.Directory.Create(); } if (info.Exists) { info.Attributes = FileAttributes.Normal; info.Delete(); } File.WriteAllBytes(path, stream.ToArray()); } }
internal Icon GetIcon(BinaryReader reader) { using (MemoryStream stream = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(stream)) { ICON_HEADER header = new ICON_HEADER(); header.idCount = Convert.ToUInt16(Entries.Count); header.idReserved = Group.idReserved; header.idType = Group.idType; writer.Write(RawSerialize(header)); long size = Marshal.SizeOf(typeof(ICON_DIRECTORY_ENTRY)); long headerEnd = Marshal.SizeOf(typeof(ICON_HEADER)) + (size * Entries.Count); long baseImageDataOffset = headerEnd; foreach (IconImage entry in Entries) { ICON_DIRECTORY_ENTRY ent = new ICON_DIRECTORY_ENTRY(); ent.ColorCount = entry.Entry.ColorCount; ent.Height = entry.Entry.Height; ent.Reserved = entry.Entry.Reserved; ent.Width = entry.Entry.Width; ent.BytesInRes = entry.Entry.BytesInRes; ent.BitCount = entry.Entry.BitCount; ent.Planes = entry.Entry.Planes; ent.ImageOffset = Convert.ToUInt32(baseImageDataOffset); baseImageDataOffset += entry.Entry.BytesInRes; writer.Write(RawSerialize(ent)); } foreach (IconImage entry in Entries) { writer.Write(entry.GetImageData(reader, m_Stream, entry.GetResourceAddress(SectionBaseAddress, SectionVirtualAddress))); } } return(BytesToIcon(stream.ToArray())); } }