public static void WriteImage(Bitmap src, Stream stream) { IcnsType imageType = IcnsType.FindType(src.Width, src.Height, 32, IcnsType.TypeDetails.None); if (imageType == null) { throw new NotSupportedException($"Invalid/unsupported source: {src.Width}x{src.Height}"); } Write4Bytes(stream, ICNS_MAGIC); Write4Bytes(stream, 4 + 4 + 4 + 4 + 4 * imageType.Width * imageType.Height + 4 + 4 + imageType.Width * imageType.Height); Write4Bytes(stream, imageType.Type); Write4Bytes(stream, 4 + 4 + 4 * imageType.Width * imageType.Height); BitmapData bitmapData = src.LockBits(new Rectangle(0, 0, src.Width, src.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); // the image for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { uint argb = IcnsDecoder.GetPixel(bitmapData, x, y); stream.WriteByte(0); stream.WriteByte((byte)((argb & 0x00ff0000) >> 16)); stream.WriteByte((byte)((argb & 0x0000ff00) >> 8)); stream.WriteByte((byte)((argb & 0x000000ff) >> 0)); } } // mask IcnsType maskType = IcnsType.FindType(src.Width, src.Height, 8, IcnsType.TypeDetails.Mask); Write4Bytes(stream, maskType.Type); Write4Bytes(stream, 4 + 4 + imageType.Width * imageType.Width); for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { uint argb = IcnsDecoder.GetPixel(bitmapData, x, y); stream.WriteByte((byte)((argb & 0xff000000) >> 24)); } } }
public static IcnsImage GetImage(Stream stream) { IcnsElement[] icnsContents = ReadImage(stream); List <IcnsImage> result = IcnsDecoder.DecodeAllImages(icnsContents); if (result.Count <= 0) { throw new NotSupportedException("No icons in ICNS file"); } IcnsImage max = null; foreach (IcnsImage bitmap in result) { if (bitmap.Bitmap != null && (max == null || (bitmap.Bitmap.Width > bitmap.Bitmap.Height))) { max = bitmap; } } return(max); }
public static List <IcnsImage> GetImages(Stream stream) { IcnsElement[] icnsContents = ReadImage(stream); return(IcnsDecoder.DecodeAllImages(icnsContents)); }