private void Deserialize(byte[] data, bool ignoreErrors) { // Deserialize headers ByteStream stream = new ByteStream(data); ICONDIR icondir = new ICONDIR(stream); ICONDIRENTRY[] icondirentries = new ICONDIRENTRY[icondir.ImageCount]; for (int i = 0; i < icondirentries.Length; i++) { icondirentries[i] = new ICONDIRENTRY(icondir.Type, stream); } this.Type = icondir.Type; // Deserialize images foreach (ICONDIRENTRY icondirentry in icondirentries) { try { this.Images.Add(new ICOImage(this.Type, icondirentry, data)); } catch (Exception e) { if (!ignoreErrors) { throw e; } } } }
public static byte[] FromICO(ICONDIRENTRY icondirentry, byte[] BMPData) { byte[] result = new byte[BITMAPFILEHEADER_SIZE + BMPData.Length]; ByteStream stream = new ByteStream(result); // Header field used to identify the BMP: "BM" in ASCII stream.Write16(BITMAPFILEHEADER_SIGNATURE); // The size of the BMP file in bytes stream.Write32(result.Length); // Reserved; actual value depends on the application that creates the image stream.Write16(0); // Reserved; actual value depends on the application that creates the image stream.Write16(0); // The offset of the byte where the bitmap image data (pixel array) can be found stream.Write32(BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_SIZE); // Copy BMPData Bytes.Replace(result, BMPData, BITMAPFILEHEADER_SIZE); // Fix BITMAPINFOHEADER height int bpp = Bytes.FromBytes(result, BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_BITSPERPIXEL_OFFSET, 2); int height = Bytes.FromBytes(result, BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_HEIGHT_OFFSET, 4); height /= 2; byte[] heightBytes = Bytes.FromInt(height, 4); Bytes.Replace(result, heightBytes, BITMAPFILEHEADER_SIZE + BITMAPINFOHEADER_HEIGHT_OFFSET); if (bpp < 32) { // TODO: Strip AND mask inside the bitmap data? } return(result); }
private byte[] SerializeHeaders() { ByteStream stream = new ByteStream(ICONDIR.SIZE + (ICONDIRENTRY.SIZE * Images.Count)); stream.Write(ICONDIR.Serialize(this.Type, this.Images.Count)); foreach (ICOImage image in Images) { stream.Write(ICONDIRENTRY.Serialize(this.Type, image, this.OffsetOfImage(image))); } return(stream.Buffer); }
internal ICOImage(ICOType type, ICONDIRENTRY icondirentry, byte[] icoData) { byte[] imageData = Bytes.Subset(icoData, icondirentry.Image.Offset, icondirentry.Image.Size); if (BMP.isStrippedBMP(imageData)) { imageData = BMP.FromICO(icondirentry, imageData); this.Type = ICOImageType.BMP; } else { this.Type = ICOImageType.PNG; } MemoryStream stream = new MemoryStream(imageData); this.Image = new Bitmap(System.Drawing.Image.FromStream(stream)); this.HotspotX = icondirentry.Image.HotspotX; this.HotspotY = icondirentry.Image.HotspotY; }