private static void SaveCor( Bitmap item, Point hotspot, Stream stream, ushort type, Color transp ) { IconFileHeader ifh = new IconFileHeader(); ifh.icoReserved = 0x0; ifh.icoResourceCount = 1; ifh.icoResourceType = type; ifh.Write( stream ); IconInfoHeader iif = new IconInfoHeader(); BITMAPINFOHEADER bih = new BITMAPINFOHEADER(); iif.Width = (byte)item.Width; iif.Height = (byte)item.Height; iif.ColorCount = 0; iif.Reserved1 = 0; iif.Reserved2 = (ushort)hotspot.X; iif.Reserved3 = (ushort)hotspot.Y; #if RGB24 int linesize = ((item.Width * 24 + 31) / 32) * 4; #else int linesize = ((item.Width * 32 + 31) / 32) * 4; #endif int linesize_mask = ((item.Width * 1 + 31) / 32) * 4; int size = linesize * item.Height + linesize_mask * item.Height + 40; #if DEBUG Console.WriteLine( "linesize=" + linesize ); #endif iif.icoDIBSize = (uint)size; iif.icoDIBOffset = 0x16; iif.Write( stream ); bih.biSize = 40; bih.biWidth = item.Width; #if RGB24 bih.biHeight = item.Height * 2; #else bih.biHeight = item.Height * 2; #endif bih.biPlanes = 1; #if RGB24 bih.biBitCount = 24; #else bih.biBitCount = 32; #endif bih.biCompression = 0; bih.biSizeImage = (uint)(linesize * item.Height); bih.biXPelsPerMeter = 0;// (int)(item.HorizontalResolution / 2.54e-2); bih.biYPelsPerMeter = 0;// (int)(item.VerticalResolution / 2.54e-2); bih.biClrUsed = 0; bih.biClrImportant = 0; bih.Write( stream ); for ( int y = item.Height - 1; y >= 0; y-- ) { int count = 0; for ( int x = 0; x < item.Width; x++ ) { Color c = item.GetPixel( x, y ); stream.WriteByte( (byte)c.B ); stream.WriteByte( (byte)c.G ); stream.WriteByte( (byte)c.R ); #if DEBUG if ( c.R != transp.R || c.G != transp.G || c.B != transp.B ) { Console.WriteLine( "color=" + c ); } #endif #if RGB24 count += 3; #else stream.WriteByte( (byte)c.A ); count += 4; #endif } for ( int i = count; i < linesize; i++ ) { stream.WriteByte( 0x0 ); } } for ( int y = item.Height - 1; y >= 0; y-- ) { int count = 0; byte v = 0x0; int tcount = 0; for ( int x = 0; x < item.Width; x++ ) { Color c = item.GetPixel( x, y ); byte tr = 0x0; if ( c.R == transp.R && c.G == transp.G && c.B == transp.B ){ tr = 0x1; } v = (byte)((byte)(v << 1) | (byte)(tr & 0x1)); tcount++; if ( tcount == 8 ) { stream.WriteByte( v ); count++; tcount = 0; v = 0x0; } } if ( 0 < tcount ) { v = (byte)(v << (9 - tcount)); stream.WriteByte( v ); count++; } for ( int i = count; i < linesize_mask; i++ ) { stream.WriteByte( 0x0 ); } } }
public static IconFileHeader Read( Stream fs ) { IconFileHeader ifh = new IconFileHeader(); byte[] buf = new byte[2]; fs.Read( buf, 0, 2 ); ifh.icoReserved = BitConverter.ToUInt16( buf, 0 ); fs.Read( buf, 0, 2 ); ifh.icoResourceType = BitConverter.ToUInt16( buf, 0 ); fs.Read( buf, 0, 2 ); ifh.icoResourceCount = BitConverter.ToUInt16( buf, 0 ); return ifh; }