PDA

View Full Version : Converting from HBitmap to a QPixmap



qtUser500
2nd March 2009, 20:38
I am using Qt3.
Would like to know how I can convert a HBitmap to a QPixmap?

Is it possible in Qt3? If not what are the alternatives?

ComaWhite
3rd March 2009, 05:21
Why don't you use Qt4 instead of old abandon Qt3?

zgulser
3rd March 2009, 06:40
Hi,

try this;

yourPixmap = QPixmap::fromWinHBitmap( pixmap, QPixmap::PremultipliedAlpha);

spirit
3rd March 2009, 06:44
Hi,

try this;

yourPixmap = QPixmap::fromWinHBitmap( pixmap, QPixmap::PremultipliedAlpha);

there is no such method in Qt3.

qtUser500
3rd March 2009, 13:43
there is no such method in Qt3.

Yes I noticed no method like that in Qt3.

Any suggestions on how to convert a HBitmap to some other image form after which it can be converted to QPixmap.

spirit
3rd March 2009, 13:56
this is code from Qt 4.4.3


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);
}

qtUser500
3rd March 2009, 17:26
Thanks Spirit for the information, I was able to resolve it.
Appreciate the time of everyone who replied to my post :)