Home
Map
PNG, WEBP Image Methods (Process.Start)Use Process.Start to improve PNG compression, and call cwebp for WEBP image support.
C#
This page was last reviewed on May 20, 2023.
PNG, WEBP images. Many image files are needlessly large. With the C# language, you can call image optimization exes like optipng.exe and cwebp.exe.
7-Zip Executable
An intro. Here we are not going to implement any advanced image algorithms. The image optimization space is extremely complicated.
Optipng. 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.
using
Here We create a new ProcessStartInfo with the filename specified to OPTIPNG. We use ProcessWindowStyle.Hidden to be more user-friendly.
Detail The Arguments property is assigned to a string that includes the PNG file to be compressed.
Property
Tip We use the -o7 option in OPTIPNG. This tells OPTIPNG to use heavy compression.
Finally The code uses Process.Start to run OPTIPNG on test.png. The end result is that test.png will be optimized with OPTIPNG.
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 = "C:\\optipng.exe"; info.WindowStyle = ProcessWindowStyle.Hidden; info.Arguments = "\"" + f + "\" -o7"; // Use Process for the application. using (Process exe = Process.Start(info)) { exe.WaitForExit(); } } }
Cwebp. In 2020, over 70% of web users have support for WebP images. This format reduces file size about 30% on average over PNG and JPG. We can call cwebp.exe to convert to WebP.
Tip We use a quality setting of 50, which does reduce the image appearance noticeably, but it leaves us with tiny files.
Also The other options, like "m" and "sns" can be changed for smaller files—much experimentation is needed.
using System.Diagnostics; class Program { static void Main() { // File paths. string f = @"C:\programs\test.png"; string f2 = @"C:\programs\test.webp"; // Run cwebp. ProcessStartInfo info = new ProcessStartInfo(); info.FileName = @"C:\cwebp.exe"; info.WindowStyle = ProcessWindowStyle.Hidden; info.Arguments = "-q 50 -m 6 -noalpha -quiet -mt -sns 100 " + f + " -o " + f2; using (Process exe = Process.Start(info)) { exe.WaitForExit(); } } }
Notes, testing. To test these programs, place an image titled test.png in the location specified. Go to Explorer in Windows and get Properties on test.png.
And Write down the byte size of the PNG image (the before size). My PNG image was 7,496 bytes.
Detail Run the C# program and wait for it to exit. It should take a fraction of a second, not counting .NET initialization time.
Finally After I ran the console program, my PNG image's size changed to 6,983 bytes.
A summary. We can control optipng.exe and cwebp.exe with C# programs. This can be used in a larger application to save bandwidth.
Webp results. For a large website, cwebp.exe compression can save many bytes per page—images are 30% smaller or more. This can reduce costs and improve user experience.
C#VB.NETPythonGoJavaSwiftRust
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2023 Sam Allen.