public unsafe void Save(Stream stream) { ICONDIR iconDir = ICONDIR.Initalizated; iconDir.idCount = (ushort)this.IconImages.Count; iconDir.Write(stream); int entryPos = sizeof(ICONDIR); // Placeholder for an array if ICONDIRENTRY here int imagesPos = entryPos + iconDir.idCount * sizeof(ICONDIRENTRY); foreach (IconImage iconImage in this.IconImages) { // IconImage stream.Seek(imagesPos, SeekOrigin.Begin); // Header BITMAPINFOHEADER header = Make_BITMAPINFOHEADER(iconImage); header.Write(stream); // Palette - (none) // XOR Image stream.Write(iconImage.XOR, 0, iconImage.XOR.Length); // AND Image - (not needed) long bytesInRes = stream.Position - imagesPos; // IconDirHeader stream.Seek(entryPos, SeekOrigin.Begin); ICONDIRENTRY iconEntry = Make_ICONDIRENTRY(iconImage); stream.Seek(entryPos, SeekOrigin.Begin); iconEntry.dwImageOffset = (uint)imagesPos; iconEntry.dwBytesInRes = (uint)bytesInRes; iconEntry.Write(stream); entryPos += sizeof(ICONDIRENTRY); imagesPos += (int)bytesInRes; } }
public unsafe void Save(MultiIcon multiIcon, Stream stream) { if (multiIcon.SelectedIndex == -1) { return; } SingleIcon singleIcon = multiIcon[multiIcon.SelectedIndex]; // ICONDIR header ICONDIR iconDir = ICONDIR.Initalizated; iconDir.idCount = (ushort)singleIcon.Count; iconDir.Write(stream); // ICONENTRIES int entryPos = sizeof(ICONDIR); int imagesPos = sizeof(ICONDIR) + iconDir.idCount * sizeof(ICONDIRENTRY); foreach (IconImage iconImage in singleIcon) { // for some formats We don't know the size until we write, // so we have to write first the image then later the header // IconImage stream.Seek(imagesPos, SeekOrigin.Begin); iconImage.Write(stream); long bytesInRes = stream.Position - imagesPos; // IconDirHeader stream.Seek(entryPos, SeekOrigin.Begin); ICONDIRENTRY iconEntry = iconImage.ICONDIRENTRY; stream.Seek(entryPos, SeekOrigin.Begin); iconEntry.dwImageOffset = (uint)imagesPos; iconEntry.dwBytesInRes = (uint)bytesInRes; iconEntry.Write(stream); entryPos += sizeof(ICONDIRENTRY); imagesPos += (int)bytesInRes; } }
public void Save(string iconFile) { FileStream output = new FileStream(iconFile, FileMode.Create, FileAccess.Write, FileShare.Read); BinaryWriter bw = null; try { bw = new BinaryWriter(output); this.writeIconFileHeader(bw); int num = 6 + (0x10 * this.iconCollection.Count); foreach (IcDvImg image in this.iconCollection) { int num2 = image.IconImageDataBytes(); ICONDIRENTRY icondirentry = new ICONDIRENTRY(); icondirentry.width = (byte)image.IconSize.Width; icondirentry.height = (byte)image.IconSize.Height; switch (image.ColorDepth) { case ColorDepth.Depth16Bit: icondirentry.colorCount = 0; icondirentry.wBitCount = 0x10; break; case ColorDepth.Depth24Bit: icondirentry.colorCount = 0; icondirentry.wBitCount = 0x18; break; case ColorDepth.Depth32Bit: icondirentry.colorCount = 0; icondirentry.wBitCount = 0x20; break; case ColorDepth.Depth4Bit: icondirentry.colorCount = 0x10; icondirentry.wBitCount = 4; break; case ColorDepth.Depth8Bit: icondirentry.colorCount = 0xff; icondirentry.wBitCount = 8; break; } icondirentry.wPlanes = 1; icondirentry.dwBytesInRes = num2; icondirentry.dwImageOffset = num; icondirentry.Write(bw); num += num2; } foreach (IcDvImg image2 in this.iconCollection) { image2.SaveIconBitmapData(bw); } } catch (Exception exception) { if (exception is SystemException) { throw exception; } throw new IconExException(exception.Message, exception); } finally { if (bw != null) { bw.Close(); } } }
/// <summary> /// Saves the icon to the specified file /// </summary> /// <param name="iconFile">File to save to</param> public void Save( string iconFile ) { // open the file for writing, truncate if exists FileStream fs = new FileStream( iconFile, FileMode.Create, FileAccess.Write, FileShare.Read); BinaryWriter bw = null; try { bw = new BinaryWriter(fs); // write out the icon header: writeIconFileHeader(bw); // write out the icon directory entries: int iconOffset = 6 + 16 * this.iconCollection.Count; foreach (IconDeviceImage idi in this.iconCollection) { int bytesInRes = idi.IconImageDataBytes(); ICONDIRENTRY ide = new ICONDIRENTRY(); ide.width = (byte)idi.IconSize.Width; ide.height = (byte)idi.IconSize.Height; switch (idi.ColorDepth) { case ColorDepth.Depth4Bit: ide.colorCount = 16; ide.wBitCount = 4; break; case ColorDepth.Depth8Bit: ide.colorCount = 0; //BUG CORRECTED, vasian, old was 255, based on http://www.daubnet.com/formats/ICO.html ide.wBitCount = 8; break; case ColorDepth.Depth16Bit: ide.colorCount = 0; ide.wBitCount = 16; break; case ColorDepth.Depth24Bit: ide.colorCount = 0; ide.wBitCount = 24; break; case ColorDepth.Depth32Bit: ide.colorCount = 0; ide.wBitCount = 32; break; } ide.wPlanes = 1; ide.dwBytesInRes = bytesInRes; ide.dwImageOffset = iconOffset; ide.Write(bw); iconOffset += bytesInRes; } // write out the icon data: foreach (IconDeviceImage idi in this.iconCollection) { idi.SaveIconBitmapData(bw); } } catch (Exception ex) { if (ex is SystemException) { throw ex; } else { throw new IconExException(ex.Message, ex); } } finally { if (bw != null) { bw.Close(); } } }
internal static string makeIconEx(string icon, int size, int depth, string icoFormat) { // Header of the whole file, the file may contain more than one Icon byte[] bytes = System.Convert.FromBase64String(icon); int colorsInPalette = depth > 8 ? 0 : 1 << depth; ICONDIR iconDir = ICONDIR.Initalizated; ICONDIRENTRY iconEntry = new ICONDIRENTRY(); BITMAPINFOHEADER bitMapHeader = new BITMAPINFOHEADER(); iconEntry.bWidth = (byte)size; iconEntry.bHeight = (byte)size; iconEntry.bColorCount = (byte)colorsInPalette; // 4 = 16 colors, 8 = 256 iconEntry.wBitCount = (ushort)depth; iconEntry.bReserved = 0; iconEntry.dwBytesInRes = (uint)(bytes.Length + Marshal.SizeOf(bitMapHeader) + colorsInPalette * 4); // Each color requires 4 bytes in the palette iconEntry.dwImageOffset = (uint)(Marshal.SizeOf(iconDir) + Marshal.SizeOf(iconEntry)); iconEntry.wPlanes = 1; bitMapHeader.biSize = (uint)Marshal.SizeOf(bitMapHeader); bitMapHeader.biWidth = (uint)size; bitMapHeader.biHeight = (uint)size * 2; bitMapHeader.biBitCount = (ushort)depth; bitMapHeader.biPlanes = 1; bitMapHeader.biCompression = IconImageFormat.BMP; bitMapHeader.biXPelsPerMeter = 0; bitMapHeader.biYPelsPerMeter = 0; bitMapHeader.biClrUsed = (uint)colorsInPalette; bitMapHeader.biClrImportant = 0; bitMapHeader.biSizeImage = (uint)bytes.Length; // // The icon is recieved flipped vertically, we need first to flip it before sending // it to the consumer // byte[] data = null; int len = 0, revIndex = 0, rowWidth = 0, ANDmapSize; len = bytes.Length; data = new byte[bytes.Length]; rowWidth = size * depth / 8; ANDmapSize = len - size * size * depth / 8; int step = 4; for (int k = len - rowWidth; k >= ANDmapSize; k -= rowWidth) { for (int r = 0; r < rowWidth; r++, revIndex++) { data[revIndex] = bytes[k + r]; } } for (int l = 0, m = ANDmapSize - step; l < ANDmapSize; l += step, m -= step) { for (int n = 0; n < step; n++, revIndex++) { data[revIndex] = bytes[m + n]; } } MemoryStream mStream = new MemoryStream(); // // Write the Icon directory // iconDir.Write(mStream); // // Write the icon directory entry // iconEntry.Write(mStream); // // Write the bitmap header // bitMapHeader.Write(mStream); // // Write the appropriate palette if required // if (depth == 4) { mStream.Write(palette_16, 0, palette_16.Length); } // // Write the icon data recieved from the CPS server // mStream.Write(data, 0, data.Length); byte[] outBytes; // // If the consumer requested an ico format return the stream as is without any conversion // if (icoFormat.ToLower() == "ico") { outBytes = mStream.GetBuffer(); mStream.Dispose(); return(Convert.ToBase64String(outBytes)); } // // Store the stream in an Icon object, this will solve the transparency problem // mStream.Position = 0; Icon iconStream = new Icon(mStream, depth, depth); // // Retrieve the icon as bitmap for conversion // Bitmap bitMap = iconStream.ToBitmap(); ImageFormat imgFormat = ImageFormat.Png; switch (icoFormat.ToLower()) { case "png": imgFormat = ImageFormat.Png; break; case "gif": imgFormat = ImageFormat.Gif; break; case "tiff": imgFormat = ImageFormat.Tiff; break; } // // Save the icon in the requested format to an icon to be sent to the consumer // MemoryStream oMStream = new MemoryStream(); bitMap.Save(oMStream, imgFormat); outBytes = oMStream.GetBuffer(); mStream.Dispose(); oMStream.Close(); oMStream.Dispose(); bitMap.Dispose(); // // Return the icon as base64 string // return(Convert.ToBase64String(outBytes)); }
/// <summary> /// Convert this IconEx to Icon /// </summary> /// <returns>icon</returns> public Icon GetIcon() { Icon icon = null; try { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); // write out the icon header: writeIconFileHeader(bw); // write out the icon directory entries: int iconOffset = 6 + 16 * this.iconCollection.Count; foreach (IconDeviceImage idi in this.iconCollection) { int bytesInRes = idi.IconImageDataBytes(); ICONDIRENTRY ide = new ICONDIRENTRY(); ide.width = (byte)idi.IconSize.Width; ide.height = (byte)idi.IconSize.Height; switch (idi.ColorDepth) { case ColorDepth.Depth4Bit: ide.colorCount = 16; ide.wBitCount = 4; break; case ColorDepth.Depth8Bit: ide.colorCount = 255; ide.wBitCount = 8; break; case ColorDepth.Depth16Bit: ide.colorCount = 0; ide.wBitCount = 16; break; case ColorDepth.Depth24Bit: ide.colorCount = 0; ide.wBitCount = 24; break; case ColorDepth.Depth32Bit: ide.colorCount = 0; ide.wBitCount = 32; break; } ide.wPlanes = 1; ide.dwBytesInRes = bytesInRes; ide.dwImageOffset = iconOffset; ide.Write(bw); iconOffset += bytesInRes; } // write out the icon data: foreach (IconDeviceImage idi in this.iconCollection) { idi.SaveIconBitmapData(bw); } ms.Position = 0; icon = new Icon(ms); } catch (Exception ex) { if (ex is SystemException) { throw ex; } else { throw new IconExException(ex.Message, ex); } } return icon; }