The Tinify API allows you to compress and optimize JPEG and PNG images. It is designed as a REST service.
New .NET client library
The official client library for Microsoft .NET is currently being developed. It will be finished in March 2017. You can download a pre-release version from GitHub and try it out.
Basic C# with .NET example
Shown below is a basic example for compressing images with C# in Microsoft .NET.
using System;
using System.Net;
using System.Text;
using System.IO;
class Program {
static void Main() {
string key = "<your api key>";
string input = "large-input.png";
string output = "tiny-output.png";
string url = "https://api.tinify.com/shrink";
WebClient client = new WebClient();
string auth = Convert.ToBase64String(Encoding.UTF8.GetBytes("api:" + key));
client.Headers.Add(HttpRequestHeader.Authorization, "Basic " + auth);
try {
client.UploadData(url, File.ReadAllBytes(input));
/* Compression was successful, retrieve output from Location header. */
client.DownloadFile(client.ResponseHeaders["Location"], output);
} catch (WebException) {
/* Something went wrong! You can parse the JSON body for details. */
Console.WriteLine("Compression failed.");
}
}
}
Need help? Got feedback?
We’re always here to help, so if you’re stuck just drop us a note on [email protected]. It’s also the perfect place to send us all your suggestions and feedback.