Ok so this was the cause of the problem
incorrect handling of the bitmap data and improper configuration of the BITMAPINFO structure
Heres is the correct code.
QPixmap groups::extractPixmapFromExe(const QString &exePath) {
QFileInfo fileInfo(exePath);
QString fileName = fileInfo.fileName();
qDebug() << "Extracting icon from file: " << fileName;
// Extract the icon from the executable
HICON hIcon = ExtractIcon(NULL, exePath.toStdWString().c_str(), 0);
if (hIcon == NULL) {
qDebug() << "Failed to extract icon from" << exePath;
return QPixmap(); // Return an empty pixmap if icon extraction fails
}
// Get icon information
ICONINFO iconInfo;
if (!GetIconInfo(hIcon, &iconInfo)) {
qDebug() << "Failed to get icon info.";
DestroyIcon(hIcon);
return QPixmap();
}
// Get the color bitmap information
BITMAP bitmap;
if (!GetObject(iconInfo.hbmColor, sizeof(bitmap), &bitmap)) {
qDebug() << "Failed to get bitmap info.";
DestroyIcon(hIcon);
return QPixmap();
}
// Create a device context for the bitmap
HDC hdc = GetDC(NULL);
HDC memDC = CreateCompatibleDC(hdc);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(memDC, iconInfo.hbmColor);
// Prepare the BITMAPINFO structure
BITMAPINFO bmi = {};
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = bitmap.bmWidth;
bmi.bmiHeader.biHeight = -bitmap.bmHeight; // Negative for top-down bitmap
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
// Allocate a buffer for the bitmap data
QByteArray buffer(bitmap.bmWidth * bitmap.bmHeight * 4, 0); // 32-bit ARGB format
// Retrieve the bitmap data
if (!GetDIBits(memDC, iconInfo.hbmColor, 0, bitmap.bmHeight, buffer.data(), &bmi, DIB_RGB_COLORS)) {
qDebug() << "Failed to retrieve bitmap data.";
SelectObject(memDC, hOldBitmap);
DeleteDC(memDC);
ReleaseDC(NULL, hdc);
DestroyIcon(hIcon);
return QPixmap();
}
// Create a QImage from the bitmap data
QImage img(reinterpret_cast<const uchar*>(buffer.constData()), bitmap.bmWidth, bitmap.bmHeight, QImage::Format_ARGB32);
// Clean up resources
SelectObject(memDC, hOldBitmap);
DeleteDC(memDC);
ReleaseDC(NULL, hdc);
DestroyIcon(hIcon);
// Convert the QImage to a QPixmap and return it
return QPixmap::fromImage(img);
}