Large size images are slow to load and optimizing them can reduce bandwidth and help your website load faster. TinyPNG is a great place to compress JPEG or PNG images. In fact, we always use TinyPNG to compress images, created for this blog. Along with web interface, they also expose REST API (Free/Paid) to compress images on the fly. In this post, let’s find out how to compress and resize/scale images in ASP.NET Core using TinyPNG API.
Compress and Resize/Scale Images in ASP.NET Core
TinyPNG uses smart lossy compression techniques to reduce the file size of your PNG files. TinyPNG uses an algorithm to reduce the number of colors in a way that’s unnoticeable to the naked eye.
By selectively decreasing the number of colors in the image, fewer bytes are required to store the data. The effect is nearly invisible but it makes a very large difference in file size!
The Tinify API allows you to compress and optimize JPEG and PNG images. It is designed as a REST service. It compresses first 500 images for free every month. You can use different TinyPNG account after 500 images and continue with your compression free of charge or you can also opt for a paid subscription. You can get the pricing details from here.
To use the API, we need the API key. To get API Key, visit this URL. Provide your name and email address to get access of developer dashboard. You can find your API key on the TinyPNG developer dashboard.
To use with ASP.NET Core, first install the Tinify nuget package.
Install-Package Tinify
Using it for compression or resizing is very simple. With two lines of code, image compression can be achieved. Just, provide the source image path to Tinify API and destination path.
public async void CompressImage() { Tinify.Key = API_KEY; //TinyPNG Developer API KEY string sFilePath = @"d:\ButterFly.jpg"; string sOptimizedFile = @"d:\ButterFlyOptimized.jpg"; var source = Tinify.FromFile(sFilePath); await source.ToFile(sOptimizedFile); }
The below image shows the size difference between both the images. The compression ratio can be up to 70%, but that depends on the image.
Resizing the image is also very simple. To resize an image, call the resize
method on an image source and supply width, height and resizing method.
public async void ResizeImage() { Tinify.Key = API_KEY; //TinyPNG Developer API KEY string sFilePath = @"d:\ButterFly.jpg"; string sOptimizedFile = @"d:\ButterFlyResized.jpg"; var source = Tinify.FromFile(sFilePath); var resized = source.Resize(new { method = "fit", width = 250, height = 250 }); await resized.ToFile(sOptimizedFile); }
The resizing method describes the way your image will be resized. The following methods are available:
- Fit: Scales the image down proportionally so that it fits within the given dimensions. You must provide both a width and a height.
- Scale: Scales the image down proportionally. You must provide either a target width or a target height, but not both.
- Cover: Scales the image proportionally and crops it if necessary so that the result has exactly the given dimensions. You must provide both a width and a height. Which parts of the image are cropped away is determined automatically.
Tinify API also helps in preserving the metadata of the image like copyright information, the GPS location and the creation date. And for error handling, the Tinify API uses HTTP status codes to indicate success or failure. Any HTTP errors are converted into exceptions, which are thrown by the client library. There are 4 distinct types of errors/exceptions are thrown.
- AccountException: Thrown when a problem with the API or your account.
- ClientException: Thrown when a problem with the submitted data.
- ServerException: Thrown because of a temporary problem with the Tinify API.
- ConnectionException: Thrown when there is an issue connecting to the Tinify API.
That’s it.
Summary
TinyPNG is indeed quite promising choice to compress and resize images because of its algorithm which literally achieves the compression which can’t be noticed with naked eyes. The REST API enables compression on the fly for web application. We saw how to use it with ASP.NET Core to compress and resize images.
Thank you for reading. Keep visiting this blog and share this in your network. Please put your thoughts and feedback in the comments section.