public void Test(string fileName, int width, int height) { string pngFilePath = TestUtil.GetResourceFilePath(fileName); using var pngStream = File.OpenRead(pngFilePath); string icoFilePath = TestUtil.GetTestFilePath(Path.GetFileName(pngFilePath) + ".ico"); using (var icoStream = File.OpenWrite(icoFilePath)) { var icoEncoder = new IcoEncoder(icoStream); icoEncoder.AddImage(new ImageInfo(pngStream, (byte)width, (byte)height, 0, 32)); icoEncoder.Commit(); } ValidateIcoFile(icoFilePath, width, height); }
private static void EncodeImageForResolution(IcoEncoder icoEncoder, WICSize resolution, string sourceImagePath) { using var sourceImageStream = File.OpenRead(sourceImagePath); var sourceImageDecoder = wic.CreateDecoderFromStream(sourceImageStream, WICDecodeOptions.WICDecodeMetadataCacheOnLoad); var pngImageStream = new MemoryStream(); var pngEncoder = wic.CreateEncoder(ContainerFormat.Png); pngEncoder.Initialize(pngImageStream, WICBitmapEncoderCacheOption.WICBitmapEncoderNoCache); var pngImage = pngEncoder.CreateNewFrame(); pngImage.Initialize(); pngImage.SetPixelFormat(WICPixelFormat.WICPixelFormat32bppBGRA); pngImage.SetSize(resolution.Width, resolution.Height); var sourceImage = sourceImageDecoder.GetFrame(0); if (sourceImage.GetSize().Equals(resolution)) { pngImage.WriteSource(sourceImage); } else { var scaler = wic.CreateBitmapScaler(); scaler.Initialize(sourceImage, resolution, WICBitmapInterpolationMode.WICBitmapInterpolationModeNearestNeighbor); pngImage.WriteSource(scaler); } pngImage.Commit(); pngEncoder.Commit(); pngImageStream.Flush(); byte width = (byte)(resolution.Width == 256 ? 0 : resolution.Width); byte height = (byte)(resolution.Height == 256 ? 0 : resolution.Height); icoEncoder.AddImage(new ImageInfo(pngImageStream, width, height, 0, 32)); }