Will adding a picture background to the cells file cause the file to be corrupted?

I have a requirement to add a background image to an Excel file, but some users have reported that the file has been corrupted and the corrupted file is empty (0KB). The version of the cells library I am using is Aspose.Cells-22.6. Below is the code for the task we performed. Is there a flaw in this code or is there a flaw in this version of the library? At present, this situation occurs sporadically and cannot be reproduced unambiguously. I hope you can help answer this question. Thank you very much.

void AddBackgroundImage(const std::string& file_path, const std::string& image_path)
{
	try {
		intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(new String(file_path.c_str()));
		intrusive_ptr<IWorksheetCollection> wsc = wb->GetIWorksheets();

		for (int i = 0; i < wb->GetIWorksheets()->GetCount(); i++) {
			intrusive_ptr<IWorksheet> ws = wsc->GetObjectByIndex(i);
			ws->SetBackgroundImage(File::ReadAllBytes(new String(image_path.c_str())));
		}

		StringPtr strSaveFilePath = new String(file_path.c_str());
		wb->Save(strSaveFilePath);
	} catch (...) {
		std::cout << "exception ...";
	}
}

@dage123
By testing with the latest version Aspose.Cells for C++ 24.1, we can obtain the correct results. Please refer to the attachment (289.3 KB).

We have not only added new features to the new C++ version, but also enhanced existing features. Especially in terms of API calls, we have adopted a new implementation that can better utilize APIs in a way similar to .NET code writing, saving a lot of time to focus on the business.

The sample code as follows:

void AddBackgroundImage()
{

	// Input directory path
	U16String inputPath("..\\Data\\ForumTest\\");
	// Output directory path
	U16String outputPath("..\\Data\\ForumTest\\");

	U16String file_path = inputPath + u"sample.xlsx";
	U16String image_path = inputPath + u"image.png";

	// Create a new workbook
	Workbook workbook(file_path);

	WorksheetCollection worksheets = workbook.GetWorksheets();

	int count = worksheets.GetCount();
	for (int i = 0; i < count; i++)
	{
		Worksheet worksheet = worksheets.Get(i);
		worksheet.SetBackgroundImage(GetDataFromFile(image_path));
	}
	
	// Saving the Excel file
	workbook.Save(outputPath + u"out_cpp.xlsx");

}

Vector<uint8_t> GetDataFromFile(const U16String& file)
{
	std::string f = file.ToUtf8();
	// open a file 
	std::ifstream fileStream(f, std::ios::binary);

	if (!fileStream.is_open()) {
		std::cerr << "Failed to open the file." << std::endl;
		return 1;
	}

	// Get file size
	fileStream.seekg(0, std::ios::end);
	std::streampos fileSize = fileStream.tellg();
	fileStream.seekg(0, std::ios::beg);

	// Read file contents into uint8_t array
	uint8_t* buffer = new uint8_t[fileSize];
	fileStream.read(reinterpret_cast<char*>(buffer), fileSize);
	fileStream.close();

	Vector<uint8_t>data(buffer, fileSize);
	delete[] buffer;

	return data;
}

Hope helps a bit.

@dage123
We used the attachment mentioned above and ran your code multiple times with versions 22.6 and 23.11, but we didn’t encounter any anomalies. It’s possible that the issue is specific to certain files, and there doesn’t seem to be a problem with the code’s usage.

Starting from version 23.12, we have refactored Aspose.Cells for C++, making it more user-friendly and feature-rich. You may also consider testing with the latest version for potential improvements. Thank you!