Horst Walter
Horst Walter

Reputation: 14081

QImage, possible to change background of resource image (png/jpg)

Can I change the background of a QImage created from a resource (png, jpg) file?

    static QPixmap changeBackground(const QString resource, Qt::GlobalColor color)
    {
        QImage i(resource); // load png/img
        i.fill(color);
        return QPixmap::fromImage(i);
    }

Result of my trial is an all filled with background color image. Any chance that I change the transparent background of the png/jpg resource file?

Upvotes: 0

Views: 3521

Answers (1)

Horst Walter
Horst Walter

Reputation: 14081

How I have done it, improvements are welcome:

QPixmap MyIconClass::changeBackground(const QString resource, Qt::GlobalColor backgroundColor)
{
    QImage resSource(resource);
    QImage destBackground(resSource.size(), QImage::Format_RGB32);
    destBackground.fill(backgroundColor);
    QPainter p(&destBackground);
    p.setCompositionMode(QPainter::CompositionMode_SourceAtop);
    p.drawImage(0, 0, resSource);
    return QPixmap::fromImage(destBackground);
}

Upvotes: 2

Related Questions