gpt4 book ai didi

c++ - 从存储在 std::vector 中的数据创建和保存图片

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:38 25 4
gpt4 key购买 nike

Qt 中是否有一种方法可以根据存储在 std::vector 中的数据轻松创建图片?我的意思是,在 vector 中,QWidget 的每个 QPointF 点都有颜色,我正在使用 QPainter 在其上绘画,但我没有不仅需要使用 vector 中的颜色在 QWidget 上绘制此图片,还需要将其另存为图片。

最佳答案

如果您知道图像的初始尺寸并且有一个包含颜色信息的 vector ,您可以执行以下操作:

// Image dimensions.
const int width = 2;
const int height = 2;
// Color information: red, green, blue, black pixels
unsigned int colorArray[width * height] =
{qRgb(255, 0, 0), qRgb(0, 255, 0), qRgb(0, 0, 255), qRgb(0, 0, 0)};
// Initialize the vector
std::vector<unsigned int> colors(colorArray, colorArray + width * height);

// Create new image with the same dimensions.
QImage img(width, height, QImage::Format_ARGB32);
// Set the pixel colors from the vector.
for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
img.setPixel(row, col, colors[row * width + col]);
}
}
// Save the resulting image.
img.save("test.png");

关于c++ - 从存储在 std::vector 中的数据创建和保存图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30049472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com