如何从QGraphicsScene/QGraphicsView创建图像文件
How to create image file from QGraphicsScene/QGraphicsView?
给定QGraphicsScene
或QGraphicsView
,是否可以创建图像文件(最好是PNG或JPG)?如果是,怎么做?
在处理了这个问题之后,这里有足够的改进来保证新的答案:
scene->clearSelection(); // Selections would also render to the file
scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
QPainter painter(&image);
scene->render(&painter);
image.save("file_name.png");
我没有尝试过,但这是如何做到这一点的想法。
您可以通过几种方式完成此操作一种形式如下:
QGraphicsView* view = new QGraphicsView(scene,this);
QString fileName = "file_name.png";
QPixmap pixMap = view->grab(view->sceneRect().toRect());
pixMap.save(fileName);
//Uses QWidget::grab function to create a pixmap and paints the QGraphicsView inside it.
另一个是使用渲染函数QGraphicsScene::render():
QImage image(fn);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
image.save("file_name.png")
grabWidget已弃用,请使用grab。你可以使用QFileDialog
QString fileName= QFileDialog::getSaveFileName(this, "Save image", QCoreApplication::applicationDirPath(), "BMP Files (*.bmp);;JPEG (*.JPEG);;PNG (*.png)" );
if (!fileName.isNull())
{
QPixmap pixMap = this->ui->graphicsView->grab();
pixMap.save(fileName);
}
相关文章:
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 在OpenCV C++中创建空白图像
- 如何快速创建一组覆盖两个图像的图像
- LoadImage 函数不适用于 ImageMagick 创建的位图图像
- Cuda 从黑/白图像创建纹理对象
- 创建位图图像未在窗口上打印
- 如何在 dlib 的 array2d 图像上从 dlib::rectangle 创建 cv::Mat<rgb_pixel> ?
- 使用 ImageMagick 从像素强度数组创建灰度图像时遇到问题
- 用FWRITE创建图像后,将交换字节
- 张量流错误:执行器无法创建内核。没有注册'Snapshot' 适用于 GPU 设备的 OpKernel 运行图像标签示例
- SDL2 不将在单独函数中创建的图像渲染到SDL_RenderPresent (C++)
- 无法释放从iPlimage创建的垫子图像
- 如何从scan0创建位图/图像
- cppwinrt中图像按钮的程序化创建
- 使用 C++ 从 16 位数据创建灰度图像
- 如何创建矩阵的矢量来存储大量的图像
- OpenGL纹理格式,为OpenGL创建图像/纹理数据
- 如何使用 C 编程创建图像数据库
- Imwrite不创建图像
- 如何从QGraphicsScene/QGraphicsView创建图像文件