static void Main(string[] args) { Console.WriteLine("Hello World!"); if (args.Length != 2) { return; } List <string> files = Directory.EnumerateFiles(args[0]).ToList(); Console.WriteLine($"Using Encoder Version: {EncoderWrapper.GetEncoderVersion()}"); foreach (string file in files) { string outfile = Path.Combine(args[1], Path.GetFileNameWithoutExtension(file) + ".webp"); Image img = Image.FromFile(file); IntPtr ptr = IntPtr.Zero; ulong size = 0L; MemoryStream ms = new MemoryStream(); ms.Seek(0L, SeekOrigin.Begin); img.Save(ms, ImageFormat.Bmp); size = EncoderWrapper.EncodeBGR(ms.ToArray(), img.Width, img.Height, img.Width * 3, 100, out ptr); byte[] buffer = new byte[(int)size]; Marshal.Copy(ptr, buffer, 0, (int)size); FileStream fs = new FileStream(outfile, FileMode.Create, FileAccess.Write); fs.Write(buffer, (int)SeekOrigin.Begin, buffer.Length); fs.Flush(); fs.Close(); fs.Dispose(); ms.Close(); ms.Dispose(); img.Dispose(); //EncoderWrapper.FreeEncoder(ptr); Console.WriteLine($"File: {Path.GetFileName(file)}.webp Was Successfully Encoded"); } }
private static void EncodeTest() { Console.WriteLine($"Using Encoder Version: {EncoderWrapper.GetEncoderVersion()}"); Image img = Image.FromFile(inputFile.FullName); IntPtr ptr = IntPtr.Zero; ulong size = 0L; MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Bmp); switch (pixelFormat) { case WebP.Net.PixelFormat.BGR: if (lossless) { size = EncoderWrapper.EncodeLosslessBGR(ms.ToArray(), img.Width, img.Height, img.Width * 3, 100, out ptr); } else { size = EncoderWrapper.EncodeBGR(ms.ToArray(), img.Width, img.Height, img.Width * 3, 100, out ptr); } break; case WebP.Net.PixelFormat.RGB: if (lossless) { size = EncoderWrapper.EncodeLosslessRGB(ms.ToArray(), img.Width, img.Height, img.Width * 3, 100, out ptr); } else { size = EncoderWrapper.EncodeRGB(ms.ToArray(), img.Width, img.Height, img.Width * 3, 100, out ptr); } break; case WebP.Net.PixelFormat.RGBA: if (lossless) { size = EncoderWrapper.EncodeLosslessRGBA(ms.ToArray(), img.Width, img.Height, img.Width * 4, 100, out ptr); } else { size = EncoderWrapper.EncodeRGBA(ms.ToArray(), img.Width, img.Height, img.Width * 4, 100, out ptr); } break; case WebP.Net.PixelFormat.BGRA: if (lossless) { size = EncoderWrapper.EncodeLosslessBGRA(ms.ToArray(), img.Width, img.Height, img.Width * 4, 100, out ptr); } else { size = EncoderWrapper.EncodeBGRA(ms.ToArray(), img.Width, img.Height, img.Width * 4, 100, out ptr); } break; } byte[] buffer = new byte[(int)size]; Marshal.Copy(ptr, buffer, 0, (int)size); FileStream fs = new FileStream(outputFile.FullName, FileMode.Create, FileAccess.Write); fs.Write(buffer, (int)SeekOrigin.Begin, buffer.Length); fs.Flush(); fs.Close(); fs.Dispose(); ms.Close(); ms.Dispose(); img.Dispose(); //EncoderWrapper.FreeEncoder(ptr); Console.WriteLine("File Was Successfully Encoded"); }