You want to use a C# console program to optimize PNG images. Many PNG images are needlessly large, costing your company more and making your product slower. With the C# language, you can develop a PNG compression function. Here we look at a way to embed a free executable and use it for compression, using the C# programming language.
=== PNG optimization program results === Program uses OPTIPNG executable. One image was tested. Before optimization: 7496 bytes After optimization: 6983 bytes [7% smaller]
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. This is because the PNG optimization space is extremely complicated and developing optimization algorithms is very challenging. Some of the best algorithms are not even open source.
Make new console program. First, please start Visual Studio 2008 from your Start menu. Go to New Project, Visual C# and then Console Application and specify a name. For the example, I have ConsoleApplication125. You can be using the Visual Studio Express editions if they are all you have.
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.
(Visit optipng.sourceforge.net.)
Use Add Existing Item in Visual Studio. Now, we need to add the OPTIPNG executable to our console application. In Visual Studio, go to Project and then Add Existing Item. In the file dialog box, change the drop down to All Files (*.*). This allows you to click and add the optipng.exe.
Change "Copy if newer" in Visual Studio. In Visual Studio projects, 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.
Your C# 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 disposed properly. In the C# programming language, 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();
}
}
}Description. What the code does is create a new ProcessStartInfo with the filename specified to OPTIPNG, which is the executable we are using. Next, it uses ProcessWindowStyle.Hidden to be more user-friendly. The Arguments property is assigned a new string of the PNG file to be compressed, with the -o7 option specified. This tells OPTIPNG to use heavy compression.
Using Process.Start. 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 that 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.
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.
Results. 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. You can see the results of the file size compression of the images on this site at the top of this document.
Here we note that the PNG files are exactly equivalent in appearance. Using tools like OPTIPNG, PNGOUT, and PNGCRUSH always create equivalent PNG images. Be aware that some mobile phones may not work well with a very small number of images compressed with PNGOUT on some settings.
Here I review some useful optimization programs. Keep reading past this table for the most extreme lossless PNG compression system in the world. These programs are all freely available on the Internet in executable binary form.
OPTIPNG 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. PNGOUT This is a closed-source but very effective PNG compression utility. However, I found that it doesn't yield the very best results all by itself, and combining other utilities with it is usually better. ADVPNG This PNG 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. PNGNQ It has an algorithm, quantizer, that uses a different approach than many utilities, and it can produce very superior results. The algorithm was implemented by Stuart Coyle.
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 very high compression ratios.
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
Here we note that 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 really amazing compression. PNGSLIM produces the best lossless compression that I have found. I recommend the Graphics Optimization Blog for more on PNGSLIM, along with many examples.
(Visit graphicsoptimization.com.)
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. Remember that Adobe Firework's main allure is web graphic development and optimization.
Here we saw how to control OPTIPNG with a C# program, which 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, which is used on this site, resulted in a file of 6,356 bytes, which is about 18% smaller than the first.