Qt QImage图片透明设置(Thinkvd开发日志)

   开发Thinkvd中的player,设置透明度用的是sdl来实现的,转换中的水印用的是png, 如何设置水印的透明度,实际上要求把图片转换成带alpha的32位即可。

实现代码:
//! [8]
void ImageComposer::recalculateResult()
{
     int alpha = 100;
    QPainter::CompositionMode mode = currentMode();

    QPainter painter(&resultImage);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.fillRect(resultImage.rect(), Qt::transparent);
    painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
    //painter.drawImage(0, 0, destinationImage);
    //painter.setCompositionMode(mode);
    painter.drawImage(0, 0, sourceImage);
    //painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
    //painter.fillRect(resultImage.rect(), Qt::white);
 //painter.fillRect(resultImage.rect(), QColor(0, 0, 0, alpha));
 
 painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
 painter.fillRect(resultImage.rect(), QColor(0, 0, 0, alpha));

    painter.end();
 resultImage.save("f:/tmp/dddd.png");

    resultLabel->setPixmap(QPixmap::fromImage(resultImage));
 }

注意:
1. painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);不能少
2.  painter.drawImage与 painter.fillRect(resultImage.rect(), QColor(0, 0, 0, alpha));顺序.

你可能感兴趣的:(C++)