This is a professional solution to optimize and compress JPG in C++. Use C++ to maximize JPG optimization/compression speed. Try it online for free!
Make large JPG files smaller without loss of quality. Delete unnecessary and unused data. Shrink the size of a JPG file in code. The C++ library provides developers with an integrated API to optimize JPG content.
C++ library is a stand-alone solution and doesn't require any third party software installed.
Lossless compression and deep content optimization made easy with 'Aspose.Words for C++'. The following example demonstrates how to optimize the content of a JPG file in C++:
dotnet add package Aspose.Words.Cpp
Copy
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto shape = builder->InsertImage(u"Input.jpg");
auto saveOptions = MakeObject<ImageSaveOptions>(SaveFormat::Jpeg);
saveOptions->set_JpegQuality(50);
shape->GetShapeRenderer()->Save(u"Output.jpg", saveOptions);
using namespace Aspose::Words;
auto doc = MakeObject<Document>(u"Input.jpg");
doc->Cleanup();
auto nodes = doc->GetChildNodes(NodeType::Shape, true);
for (const auto& shape : System::IterateOver<Shape>(nodes))
{
if (shape->get_HasImage())
{
// It's up to developer to choose the library for image compression.
auto image =
System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream());
// ...
// Compress image and set it back to the shape.
shape->get_ImageData()->SetImage("yourCompressedImage");
}
}
auto saveOptions = MakeObject<PdfSaveOptions>();
saveOptions->set_CacheBackgroundGraphics(true);
doc->Save(u"Output.jpg", saveOptions);
using namespace Aspose::Words;
auto doc = MakeObject<Document>(u"Input.jpg");
doc->Cleanup();
auto nodes = doc->GetChildNodes(NodeType::Shape, true);
for (const auto& shape : System::IterateOver<Shape>(nodes))
{
if (shape->get_HasImage())
{
// It's up to developer to choose the library for image compression.
auto image = System::Drawing::Image::Fromstream(shape->get_ImageData()->ToStream());
// ...
// Compress image and set it back to the shape.
shape->get_ImageData()->SetImage("yourCompressedImage");
}
}
doc->Save(u"Output.jpg");
using namespace Aspose::Words;
auto doc = MakeObject<Document>(u"Input.jpg");
doc->Cleanup();
auto nodes = doc->GetChildNodes(NodeType::Shape, true);
for (const auto& shape : System::IterateOver<Shape>(nodes))
{
if (shape->get_HasImage())
{
// It's up to developer to choose the library for image compression.
auto image =
System::Drawing::Image::FromStream(shape->get_ImageData()->ToStream());
// ...
// Compress image and set it back to the shape.
shape->get_ImageData()->SetImage("yourCompressedImage");
}
}
auto saveOptions = MakeObject<OoxmlSaveOptions>();
saveOptions->set_CompressionLevel(CompressionLevel::Maximum);
doc->Save(u"Output.jpg", saveOptions);
using namespace Aspose::Words;
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
auto shape = builder->InsertImage(u"Input.jpg");
auto saveOptions = MakeObject<ImageSaveOptions>(SaveFormat::Jpeg);
saveOptions->set_JpegQuality(50);
shape->GetShapeRenderer()->Save(u"Output.jpg", saveOptions);
There are three options to install Aspose.Words for C++ to your developer environment. Please choose one that resembles your needs and follow the step-by-step instructions:
You can use this C++ library to develop software on Microsoft Windows, Linux and macOS operating systems:
If you develop software for Linux or macOS, please check information on additional library dependencies (fontconfig and mesa-glu open-source packages) in Product Documentation.
You can optimize files in many other file formats: