Qt-如何在单个圆形图像中合并四个图像

时间:2015-05-31 06:47:28

标签: c++ image qt qpainter qimage

我试图以圆形形式显示头像图像。它运作良好。

QImage image(":/images/person1.png");

if (image.width() != CGlobalZone::AVATAR_WIDTH_M ||
        image.height() != CGlobalZone::AVATAR_HEIGHT_M)
    image = image.scaled(QSize(CGlobalZone::AVATAR_WIDTH_M , CGlobalZone::AVATAR_HEIGHT_M ),
                         Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);

QImage roundedImage(CGlobalZone::AVATAR_WIDTH_M, CGlobalZone::AVATAR_HEIGHT_M, QImage::Format_ARGB32);

roundedImage.fill(Qt::transparent);

QBrush brush(image);
QPainter painter(&roundedImage);
QPen pen(QColor(176, 216, 242), 1);

painter.setPen(pen);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(brush);
painter.drawEllipse(1.0, 1.0, CGlobalZone::AVATAR_WIDTH_M - 2, CGlobalZone::AVATAR_HEIGHT_M - 2);

m_defaultAvatar = QPixmap::fromImage(roundedImage);

AVATAR_WIDTH_M & AVATAR_HEIGHT_M 是公共变量。
这里的结果是什么:
enter image description here
现在,我想通过合并四张照片将其更改为圆形图像。我改变了这一行

    image.height() != CGlobalZone::AVATAR_HEIGHT_M)
image = image.scaled(QSize(CGlobalZone::AVATAR_WIDTH_M , CGlobalZone::AVATAR_HEIGHT_M ),
                     Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);

对此:

   image.height() != CGlobalZone::AVATAR_HEIGHT_M)
image = image.scaled(QSize(CGlobalZone::AVATAR_WIDTH_M / 2, CGlobalZone::AVATAR_HEIGHT_M /2),
                     Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);

结果是:
enter image description here
这对我有好处,但还不够。目标是在这个圆形化身中使用四个不同的图像而不是单个图像。如何更改上述代码以合并
    QImage图像(":/ images / person1.png");

   QImage image2(":/ images / person2.png");

   QImage图像(":/ images / person3.png");

   QImage image4(":/ images / person4.png");

1 个答案:

答案 0 :(得分:0)

为什么不首先合并四个图像然后舍入合并的图像?

// Create the merged image
QImage mergedImage(2*AVATAR_WIDTH_M, AVATAR_HEIGHT_M ,QImage::Format_RGB32);
QPainter painter;
painter.begin(&combinedImage);
painter.drawImage(0, 0, image.scaled(AVATAR_WIDTH_M,AVATAR_HEIGHT_M));
painter.drawImage(AVATAR_WIDTH_M, 0, image2.scaled(AVATAR_WIDTH_M,AVATAR_HEIGHT_M));
painter.drawImage(0, AVATAR_HEIGHT_M, image3.scaled(AVATAR_WIDTH_M,AVATAR_HEIGHT_M));
painter.drawImage(AVATAR_WIDTH_M, AVATAR_HEIGHT_M, image4.scaled(AVATAR_WIDTH_M,AVATAR_HEIGHT_M));
    painter.end();

// Round the merged image...