private static void PNGCompress(string fileName) { PNGCompressor compressor = new PNGCompressor(); LosslessInputSettings inputSettings = new LosslessInputSettings(); inputSettings.OptimizationLevel = OptimizationLevel.Level1; compressor.CompressImageLossLess(fileName, "temp." + fileName, inputSettings); FileInfo file = new FileInfo(fileName); file.Delete(); file = new FileInfo("temp." + fileName); file.MoveTo(fileName); completeCount++; }
private static void PreCompress() { Bitmap bitmap = new Bitmap(60, 60); Graphics graphics = Graphics.FromImage(bitmap); graphics.Clear(Color.Aquamarine); graphics.Save(); graphics.Dispose(); bitmap.Save("pre.bmp", ImageFormat.Bmp); PNGCompressor compressor = new PNGCompressor(); LosslessInputSettings inputSettings = new LosslessInputSettings(); inputSettings.OptimizationLevel = OptimizationLevel.Level0; compressor.CompressImageLossLess("pre.bmp", "pre.png", inputSettings); RemoveTemp("pre.bmp"); RemoveTemp("pre.png"); completeCount = 0; }
static void Main(string[] args) { if (args.Length >= 3) { string pngPath = args[0]; string assetsFolder = args[1]; string stickerPack = args[2]; // Are the argument file and folder valid? if (!File.Exists(pngPath) || !Directory.Exists(assetsFolder)) { Console.WriteLine("[ ! ] Invalid argument"); return; } // Acquire file name without extension and the folder where the converted sticker should be stored string baseName = Path.GetFileName(pngPath); baseName = baseName.Substring(0, baseName.LastIndexOf('.')); string webpFolder = Path.Combine(assetsFolder, stickerPack); Console.WriteLine("[ * ] Generating sticker from: {0}", pngPath); // Is a PNG? if (!pngPath.EndsWith(".png")) { Console.WriteLine("[ ! ] File is not a PNG"); return; } // Is a 512x512 image? using (var image = System.Drawing.Image.FromFile(pngPath)) { if (image.Width != 512 || image.Height != 512) { Console.WriteLine("[ ! ] Invalid sticker size, should be 512x512"); return; } } string tmpPath = Path.Combine(webpFolder, baseName + ".tmp"); // Optimize PNG image var compressor = new PNGCompressor(); compressor.CompressImageLossy(pngPath, tmpPath); // Convert to WebP var newName = baseName + ".webp"; using (var imageFactory = new ImageFactory(preserveExifData: false)) { ISupportedImageFormat webp = new WebPFormat { Quality = 75 }; imageFactory.Load(tmpPath); imageFactory.Format(webp); imageFactory.Save(Path.Combine(webpFolder, newName)); } Console.WriteLine("[ * ] Saved WebP to: {0}", webpFolder); // Delete compressed temporary file if (File.Exists(tmpPath)) { File.Delete(tmpPath); } // Add to JSON JObject stickerJson = JObject.Parse(File.ReadAllText(Path.Combine(assetsFolder, "contents.json"))); foreach (var pack in stickerJson["sticker_packs"]) { if (pack["identifier"].ToString() == stickerPack) { JObject sticker = new JObject(); sticker["image_file"] = newName; sticker["emojis"] = new JArray(); JArray stickers = (JArray)pack["stickers"]; stickers.Add(sticker); } } File.WriteAllText(Path.Combine(assetsFolder, "contents.json"), stickerJson.ToString()); } else { Console.WriteLine(".\\ZapArtist.exe <png image> <asset folder> <sticker pack>"); } }