C# PNG Optimization

PNG images can be optimized. Many are needlessly large. These unoptimized images cost your company more and making your product slower. With the C# language, you can develop a PNG compression function that calls executables.
Intro. First, what we are going to do is embed the OPTIPNG.EXE executable within a Visual Studio project. Then, we will use C# to control that executable. We are not going to implement any advanced image algorithms.

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]
Make new console program. First, please start Visual Studio from your Start menu. Go to New Project, Visual C# and then Console Application and specify a name. For the example, I have ConsoleApplication125.
Now, we need to add the OPTIPNG executable to our application. In Visual Studio, go to Project and then Add Existing Item. In the dialog box, change the drop-down to All Files (*.*). This allows you to click and add the optipng.exe.
Change "Copy if newer." You need to right-click on the optipng.exe file and select Properties, then Copy to Output Directory, and then "Copy if newer". This means the exe will always be in the same directory as your app.
Example. Your code needs to use ProcessStartInfo, as well as Process and Process.Start. We can use the using statement around Process to make sure the resources are properly disposed. The using statement is implemented as a try-finally pattern.

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(); } } }
Test program. To test this program, put an image titled test.png in your C:\\ directory, or wherever the path specifies. Go to Explorer and get properties on test.png. Write down the byte of the PNG before. My PNG image was 7,496 bytes.

Now: Run the C# program and wait for it to exit. It should take a fraction of a second, not counting .NET initialization time.

After I ran the console program, my PNG image's size changed to 6,983 bytes. That's a savings of about 7%, on an image that was already compressed with Adobe Fireworks. The results are shown at the top.

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.

Summary. Here we saw how to control OPTIPNG with a C# program. This can be used in a larger application to save 7% or more of your bandwidth. I shared some notes on other utilities, and a way to embed any EXE in C# programs.

Finally: The best compression on the example image resulted in a file of 6,356 bytes, which is about 18% smaller than the first.

© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to
Dot Net Perls