C++ addPicture2 from a resource fails

I fail to use the function wxBook::addPicture2 to add an image from a resource.

wxImage img = loadImageFromFromResource("r_LogoExcel", "JPG"); // image is okay
unsigned char* arr = img.GetData(); // return value points to an array of characters in RGBRGBRGB
wxSize imgSz = img.GetSize();
int arrSize = imgSz.x * imgSz.y * 3 * sizeof(char); // correct size 30492
char* arr2 = reinterpret_cast<char*>(arr);
int logoId2 = book->addPicture2(arr2, arrSize); // returns -1

What is wrong here?

Please note that GetData() on the wxImage class would return the data with the pixels.

You need to pass a memory block with JPEG content, not load the JPEG and decompress it to a bitmap.

1 Like

Excellent tip, thank you!
For anyone with the same problem, this works:

HRSRC hResource = FindResource(nullptr, L"resourceName", L"JPG");
size_t _size = SizeofResource(nullptr, hResource);
HGLOBAL hMemory = LoadResource(nullptr, hResource);
LPVOID ptr = LockResource(hMemory);

const char* cArr = static_cast<char*>(hMemory);
int logoId = book->addPicture2(cArr, _size);

with an entrance in the project´s .rc file

resourceName          JPG                     "C:\\path\\to\\file\\imageFile.jpg"
2 Likes