Ejemplo n.º 1
0
        private static byte[] ReprocessPngFile(byte[] png, ParseContext context, CommandLineOptions opts)
        {
            var tempFilePath = Path.Combine(Path.GetTempPath(), "IcoCrush" + Guid.NewGuid().ToString("N") + ".png");

            File.WriteAllBytes(tempFilePath, png);

            var tokens = opts.PngToolPath.Split(' ').ToList();
            var exe    = tokens[0];

            tokens.RemoveAt(0);
            tokens.Add(tempFilePath);

            var info = new ProcessStartInfo
            {
                FileName    = exe,
                Arguments   = string.Join(' ', tokens),
                WindowStyle = ProcessWindowStyle.Hidden,
            };

            System.Console.WriteLine(info.FileName);
            System.Console.WriteLine(info.Arguments);
            using (var process = Process.Start(info))
            {
                process.WaitForExit();
                if (process.ExitCode != 0)
                {
                    throw new Exception($"PNG process returned error code {process.ExitCode}.  Process: {info.FileName} {info.Arguments}");
                }
            }

            var result = File.ReadAllBytes(tempFilePath);

            File.Delete(tempFilePath);

            var outputEncoding = PngDecoder.GetPngFileEncoding(new Memory <byte>(result));

            if (outputEncoding.ColorType != PngColorType.RGBA)
            {
                System.Console.WriteLine($"Warning: After running the PNG through the external tool, the color type is: {outputEncoding.ColorType}.  ICO files require color type RGBA for embedded PNGs.");
            }
            else if (outputEncoding.BitsPerChannel != 8)
            {
                System.Console.WriteLine($"Warning: After running the PNG through the external tool, the color depth is: {outputEncoding.BitsPerChannel * 4}-bit.  ICO files require 32-bit color depth for embedded PNGs.");
            }

            return(result);
        }
Ejemplo n.º 2
0
        private static void ReadPngFile(CommandLineOptions opts, ParseContext context, IcoFrame frame, byte[] sourceFile, out bool canSourceBePreserved)
        {
            using (var stream = new MemoryStream(sourceFile))
            {
                var decoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder();
                frame.CookedData = decoder.Decode <Rgba32>(new Configuration(), stream);
            }

            var encoding = PngDecoder.GetPngFileEncoding(new Memory <byte>(sourceFile));

            frame.Encoding.Type = IcoEncodingType.Png;

            frame.Encoding.ClaimedBitDepth = 32;
            frame.Encoding.ActualBitDepth  = 32;

            canSourceBePreserved = encoding.ColorType == PngColorType.RGBA && encoding.BitsPerChannel == 8;
        }