C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Note: The PNG optimization space is extremely complicated. Developing optimization algorithms is challenging.
PNG optimization program results
Before optimization: 7496 bytes
After optimization: 6983 bytes [7% smaller]
Here: We create a new ProcessStartInfo with the filename specified to OPTIPNG. We use ProcessWindowStyle.Hidden to be more user-friendly.
Arguments: The Arguments property is assigned a new string of the PNG file to be compressed.
Tip: We use the -o7 option in OPTIPNG. This tells OPTIPNG to use heavy compression.
Finally: It uses Process.Start to run OPTIPNG on C:\\test.png. The end result is that test.png will be optimized with OPTIPNG.
C# program that uses ProcessStartInfo
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
// PNG file path.
string f = "C:\\test.png";
// Run OPTIPNG with level 7 compression.
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "optipng.exe";
info.WindowStyle = ProcessWindowStyle.Hidden;
info.Arguments = "\"" + f + "\" -o7";
// Use Process for the application.
using (Process exe = Process.Start(info))
{
exe.WaitForExit();
}
}
}
Now: Run the C# program and wait for it to exit. It should take a fraction of a second, not counting .NET initialization time.
Validation: The PNG files are exactly equivalent in appearance. Using tools like these always create equivalent PNG images.
Caution: Be aware that some mobile phones may not work well with a small number of images compressed with PNGOUT on some settings.
Finally: The best compression on the example image resulted in a file of 6,356 bytes, which is about 18% smaller than the first.