C# PNG Optimization

Program icon

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.

PNG optimization program results

Before optimization:  7496 bytes
After optimization:   6983 bytes [7% smaller]

Intro

Steps

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.

One: 1

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.

Tip:You can be using the Visual Studio Express editions if they are all you have.

Visual Studio

Add OPTIPNG from sourceforge.net. To prepare for our program, we need to download OPTIPNG from the SourceForge website. Click on the Windows Executable, optipng-0.6.2-exe.zip. Please see the link below.

Visual Studio logo

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.

Screenshot of Solution Explorer in Visual Studio: OPTIPNG

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.

Program that uses ProcessStartInfo: C#

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();
	}
    }
}
Squares

We create a new ProcessStartInfo with the filename specified to OPTIPNG. We use ProcessWindowStyle.Hidden to be more user-friendly. 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.

Note:The file name here, C:\\test.png, must be set by you either manually or with logic. It is just a custom path to a PNG.

Test program

Note

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.

Logo

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.

EXE programs

Performance optimization

Here I review some useful optimization programs. Keep reading past this table for the most extreme lossless PNG compression system. These programs are all freely available on the Internet in executable binary form.

OPTIPNG utility. This utility is a recently-updated project, which has many of the PNGCRUSH abilities. However, in my benchmarks, PNGCRUSH can still save a few bytes from each image after OPTIPNG runs.

OptiPNG: Advanced PNG Optimizer

Next, PNGOUT is a closed-source but effective PNG compression utility. However, I found it doesn't yield the best results all by itself, and combining other utilities with it is usually better.

Ken Silverman's Utility Page

The ADVPNG recompressor, written by Andrea Mazzoleni, takes a PNG image that has already been compressed, and then recompressed that part using 7-Zip. I had excellent results with this utility.

AdvanceCOMP

Finally, PNGNQ has an algorithm, quantizer, that uses a different approach than many utilities, and it can produce superior results. The algorithm was implemented by Stuart Coyle.

Pngnq

Command lines

Console screenshot

Here we look at some example PNG compression command lines. If you just need to see some sample command lines for the above programs, I have some notes on the actual input required. These commands yield high compression ratios.

Example command lines

optipng.exe file.png -o7
pngcrush.exe file.png -brute
advpng.exe -z -4 file.png
pngout.exe file.png /y /r
pngnq.exe file.png

Extreme

Programming tip

For the most extreme PNG compression, there is a tool named PNGSLIM. It is a batch file Windows that runs hundreds of trials over your PNG to achieve amazing compression. It produces the best lossless compression I have found.

Note:The link to the blog article is no longer working so was removed. Please consider a Google search.

Using PNGSLIM. For my sites, I use a slightly modified PNGSLIM script that is faster. It routinely saves 12% or more off of images saved from Adobe Fireworks. Adobe Firework's main allure is web graphic development and optimization.

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 an example of how 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.


C#: Compression