在Qt中更改透明图像的颜色

时间:2014-03-03 10:48:24

标签: qt qimage

我在Qt中的视频上覆盖了透明图像(QImage)。我想只在点击按钮时更改透明图像的颜色。有人能告诉我怎么做吗?

谢谢。

1 个答案:

答案 0 :(得分:6)

这可以通过多种方式完成。我建议使用QPainter来创建新图像。如果您设置了SourceIn合成模式,则起始图像的Alpha通道将应用于您将要执行的任何绘图。您只需要用所需的颜色填充图像。

QPixmap source_image; // should be preserved in a class member variable
QRgb base_color; // desired image color

QPixmap new_image = source_image;
QPainter painter(&new_image);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.fillRect(new_image.rect(), base_color);
painter.end();

ui->label->setPixmap(new_image); // showing result

请注意,我使用QPixmap代替QImage,因为QPixmap更有效地显示(并且可能会绘制)。如果您出于某种原因仍想使用QImage,则此代码将与QImage一起使用而不做任何更改(当然不包括最后一行)。

来源图片:source image 结果:result

相关问题