How to Change the DPI of an Image using C/C++

Cloudmersive
2 min readAug 10, 2023

--

One simple way to increase or decrease the quality of an image is to influence the DPI (dots per inch) of the image in question. We can accomplish this manually on a one-off basis, but when we want to carry out this operation at scale, we should rely on more efficient programmatic solutions.

The ready-to-run code below makes it easy to take advantage of a free API that allows you to set the DPI of any image file via multipart/form-data. You can simply input your desired DPI in the initial request parameter and quickly generate your desired result.

To structure your API call, start by installing libcurl in your C/C++ project:

libcurl/7.75.0

After that, copy the below code examples and authenticate your request with a free-tier Cloudmersive API key (this allows up to 800 API calls per month with no commitment):

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(curl, CURLOPT_URL, "https://api.cloudmersive.com/convert/image/set-dpi/%3Cinteger%3E");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: multipart/form-data");
headers = curl_slist_append(headers, "Apikey: YOUR-API-KEY-HERE");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_mime *mime;
curl_mimepart *part;
mime = curl_mime_init(curl);
part = curl_mime_addpart(mime);
curl_mime_name(part, "inputFile");
curl_mime_filedata(part, "/path/to/file");
curl_easy_setopt(curl, CURLOPT_MIMEPOST, mime);
res = curl_easy_perform(curl);
curl_mime_free(mime);
}
curl_easy_cleanup(curl);

Yup, it’s just that easy — no more code required!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet