QT-有没有将几个图像组合成一个的类
QT - Is there any class for combine few images into one?
我想做一些图像矩阵,在一些小部件中显示预览,然后将其保存到(例如)jpg
文件中。我知道我可以把每个图像像素复制成一个大像素,但我想这不是一个有效的方法。。。有更好的解决方案吗?
谢谢你的建议。
我不复制单个像素,而是直接在足够大的QPixmap
上绘制每个单独的图像,以容纳所有图像。然后可以通过在拼贴画上绘制每个单独的图像来生成拼贴画,如下所示(未经测试的代码):
QList<QPixmap> images;
QPixmap collage;
// Make sure to resize collage to be able to fit all images.
...
for (QList<QPixmap>::const_iterator it = images.begin(); it != images.end(); ++it)
{
int x = 0;
int y = 0;
// Calculate x & y coordinates for the current image in the collage.
...
QPainter painter(&collage);
painter.drawPixmap(
QRectF(x, y, (*it).width(), (*it).height()), *it,
QRectF(0, 0, (*it).width(), (*it).height()));
}
注意,也可以使用QImage
来代替QPixmap
。QPixmap
针对屏幕显示进行了优化。有关更多详细信息,请参阅Qt文档。
不,您不想逐个像素地执行此操作。QImage是一个QPaintDevice。因此,您可以加载它们,将它们相互渲染,并根据需要以多种格式保存。当然在屏幕上显示它们。
上面的代码对我不起作用,我不知道为什么。
我需要的是一幅拼贴画:
图片A|PicB|图片…|…|
我在QImages中发现了类似的东西,这段代码也能正常工作。
(测试代码):
const int S_iconSize = 80; //The pictures are all quadratic.
QList<const QPixmap*> t_images;//list with all the Pictures, PicA, PicB, Pic...
QImage resultImage(S_iconSize*t_images.size(), S_iconSize, QImage::Format_ARGB32_Premultiplied);
QPainter painter;
painter.begin(&resultImage);
for(int i=0; i < t_images.size(); ++i)
{
painter.drawImage(S_iconSize*i, 0, t_images.at(i)->toImage(), 0, 0, S_iconSize, S_iconSize, Qt::AutoColor);
}
painter.end();
QPixmap resultingCollagePixmap = QPixmap::fromImage(resultImage);
我知道这很难看,因为QImages被转换为QPixmap,反之亦然,但它是有效的。因此,如果有人知道如何运行上面的代码(来自Ton van den Heuvel),我会很高兴。(也许只是缺少一个QPainter??)
问候
我做了以下操作:
// Load the images in order to have the sizes at hand.
QPixmap firstPixmap(":/images/first.png");
QPixmap secondPixmap(":/images/second.png");
const int gap = 25;
// Create an image with alpha channel large enough to contain them both and the gap between them.
QImage image(firstPixmap.width() + gap + secondPixmap.width(), firstPixmap.height(), QImage::Format_ARGB32_Premultiplied);
// I happen to need it transparent.
image.fill(QColor(Qt::transparent));
// Paint everything in a pixmap.
QPixmap pixmap = QPixmap::fromImage(image);
QPainter paint(&pixmap);
paint.drawPixmap(0, 0, firstPixmap);
paint.drawPixmap(firstPixmap.width() + gap, 0, secondPixmap);
// Use it.
QIcon icon(pixmap);
[...]
相关文章:
- 我需要将多个函数组合为一个函数
- 我在C++中使用提升哈希函数将 3 个双精度组合成一个面临冲突的哈希
- 找到 x^n 的所有组合,并检查它们的总和是否等于一个不包括相同数字的数字
- 如何将这两个函数组合成一个实现?
- 在 c 中给定一个固定数的情况下,找到所有可能的加法和组合(给定一个总和,找到它的可能的加法和排列
- 如何将两个代码组合成一个函数?
- 如何组合一个宽字符字符串,中间插入一些空字符
- 在C++中将返回unique_ptr和shared_ptr的两个工厂方法组合为一个
- 如何简化代码并将开关组合成一个功能?
- 如何将数组的每个n个元素组合成一个元素
- 如果我们从每个 Y 向量中选择一个值,则 X 数字的每个组合都可能
- 将 libstdc++ 和其他函数组合到一个库中
- 组合理论的一个问题
- 在编译时将整数和分数部分宏组合成一个新的宏或双精度
- 如何将一个 while 循环与 for 循环组合在一起?
- 将多个向量(函数结果)组合到一个使用模板中
- 如何将多个可变参数模板元组类组合成一个类?
- C++ STL 下一个组合排列
- 从一个组合框中选择项目,然后从其他组合框中删除该项目
- 将组合框设置为另一个组合框的ID