{
// Verify size
BITMAP bitmap_info;
memset(&bitmap_info, 0, sizeof(BITMAP));
int res;
QT_WA({
res = GetObjectW(bitmap, sizeof(BITMAP), &bitmap_info);
} , {
res = GetObjectA(bitmap, sizeof(BITMAP), &bitmap_info);
});
if (!res) {
qErrnoWarning("QPixmap::fromWinHBITMAP(), failed to get bitmap info");
}
int w = bitmap_info.bmWidth;
int h = bitmap_info.bmHeight;
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = w;
bmi.bmiHeader.biHeight = -h;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = w * h * 4;
// Get bitmap bits
uchar *data = (uchar *) qMalloc(bmi.bmiHeader.biSizeImage);
if (GetDIBits(qt_win_display_dc(), bitmap, 0, h, data, &bmi, DIB_RGB_COLORS)) {
QImage::Format imageFormat
= QImage::Format_ARGB32_Premultiplied;
uint mask = 0;
if (format == NoAlpha) {
imageFormat
= QImage::Format_RGB32;
mask = 0xff000000;
}
// Create image and copy data into image.
QImage image
(w, h, imageFormat
);
if (!image.isNull()) { // failed to alloc?
int bytes_per_line = w * sizeof(QRgb);
for (int y=0; y<h; ++y) {
QRgb *dest = (QRgb *) image.scanLine(y);
const QRgb *src = (const QRgb *) (data + y * bytes_per_line);
for (int x=0; x<w; ++x) {
dest[x] = src[x] | mask;
}
}
}
result = image;
} else {
qWarning("QPixmap::fromWinHBITMAP(), failed to get bitmap bits");
}
qFree(data);
return fromImage(result);
}
QPixmap QPixmap::fromWinHBITMAP(HBITMAP bitmap, HBitmapFormat format)
{
// Verify size
BITMAP bitmap_info;
memset(&bitmap_info, 0, sizeof(BITMAP));
int res;
QT_WA({
res = GetObjectW(bitmap, sizeof(BITMAP), &bitmap_info);
} , {
res = GetObjectA(bitmap, sizeof(BITMAP), &bitmap_info);
});
if (!res) {
qErrnoWarning("QPixmap::fromWinHBITMAP(), failed to get bitmap info");
return QPixmap();
}
int w = bitmap_info.bmWidth;
int h = bitmap_info.bmHeight;
BITMAPINFO bmi;
memset(&bmi, 0, sizeof(bmi));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = w;
bmi.bmiHeader.biHeight = -h;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = w * h * 4;
QImage result;
// Get bitmap bits
uchar *data = (uchar *) qMalloc(bmi.bmiHeader.biSizeImage);
if (GetDIBits(qt_win_display_dc(), bitmap, 0, h, data, &bmi, DIB_RGB_COLORS)) {
QImage::Format imageFormat = QImage::Format_ARGB32_Premultiplied;
uint mask = 0;
if (format == NoAlpha) {
imageFormat = QImage::Format_RGB32;
mask = 0xff000000;
}
// Create image and copy data into image.
QImage image(w, h, imageFormat);
if (!image.isNull()) { // failed to alloc?
int bytes_per_line = w * sizeof(QRgb);
for (int y=0; y<h; ++y) {
QRgb *dest = (QRgb *) image.scanLine(y);
const QRgb *src = (const QRgb *) (data + y * bytes_per_line);
for (int x=0; x<w; ++x) {
dest[x] = src[x] | mask;
}
}
}
result = image;
} else {
qWarning("QPixmap::fromWinHBITMAP(), failed to get bitmap bits");
}
qFree(data);
return fromImage(result);
}
To copy to clipboard, switch view to plain text mode
Bookmarks