如何将画板程序画出的图像保存为图片文件

时间:2022-09-26 11:49:36

~我做了画板程序,现在还剩个保存文件的功能没做,没找到什么好的办法,求大家帮助~
画板程序是用QGraphicsScene做的,注意不是用QPainter(网上有使用QPainter的时候,保存文件的方法)
现在想将画好的图像保存起来,在使用QGraphicsScene的情况下,有什么好的方法吗?找了很久也没太好的API

14 个解决方案

#1


void QGraphicsScene::render(QPainter *painter, const QRectF &target, const QRectF &source,Qt::AspectRatioMode aspectRatioMode)

#2


引用楼主 soul_ 的回复:
~我做了画板程序,现在还剩个保存文件的功能没做,没找到什么好的办法,求大家帮助~
画板程序是用QGraphicsScene做的,注意不是用QPainter(网上有使用QPainter的时候,保存文件的方法)
现在想将画好的图像保存起来,在使用QGraphicsScene的情况下,有什么好的方法吗?找了很久也没太好的API

如果你不介意使用 QPainter,问题倒很简单。

 楼上已经给出关键函数了,我给个具体例子吧:
        QImage image(mysize,QImage::Format_RGB32);
        QPainter painter(&image);
        myscene->render(&painter);
        image.save(mypath/myimagefile.png);

如果你坚持不用 QPainter,真的挺难办。(我想很多人可能和我一样,想知道你不用QPainter的理由)

#3


引用 1 楼 yanj20 的回复:
void QGraphicsScene::render(QPainter *painter, const QRectF &target, const QRectF &source,Qt::AspectRatioMode aspectRatioMode)

不是太会用这个,试了一下保存下来的图片是全黑的
代码我是这样写的:
    QString path = QFileDialog::getSaveFileName(this, tr("Save File"),
    "",tr("Images (*.png *.xpm *.jpg)"));
    QPixmap pix(1000,1000);
    QPainter p(&pix);
    scene->render(&p);
    p.end();
    pix.save(path);
结果不对。当前scene已经添加完其他item了

#4


vb dsaf asf 

#5


引用 2 楼 dbzhang800 的回复:
引用楼主 soul_ 的回复:
~我做了画板程序,现在还剩个保存文件的功能没做,没找到什么好的办法,求大家帮助~
画板程序是用QGraphicsScene做的,注意不是用QPainter(网上有使用QPainter的时候,保存文件的方法)
现在想将画好的图像保存起来,在使用QGraphicsScene的情况下,有什么好的方法吗?找了很久也没太好的API

如果你不介意使用 QPaint……


我在做个roadmap练习,指定了要用GraphicsView框架 来写这个画板

#6


引用 2 楼 dbzhang800 的回复:
如果你不介意使用 QPainter,问题倒很简单。

 楼上已经给出关键函数了,我给个具体例子吧:
  QImage image(mysize,QImage::Format_RGB32);
  QPainter painter(&image);
  myscene->render(&painter);
  image.save(mypath/myimagefile.png);

如果你坚持不用 QPainter,真的挺难办。(我想很多人可能和我一样,想知道你不用QPainter的理由)


同样用QImage也是全黑的 没有显示scene上的图像= =!

#7


是否应该将myscene->render(&painter);这句话放在scene添加item之前(就是开始画图的时候)?

#8


引用 6 楼 soul_ 的回复:
引用 2 楼 dbzhang800 的回复:

如果你不介意使用 QPainter,问题倒很简单。

楼上已经给出关键函数了,我给个具体例子吧:
QImage image(mysize,QImage::Format_RGB32);
QPainter painter(&image);
myscene->render(&painter);
image.save(myp……

你确保细节我和贴的一样? 尽管我保存图片时加了.png(支持透明度),但QImage的格式选择的 RGB32(不支持透明度)

#9


http://www.beiww.com/doc/oss/smart-questions.html

#10


void MainWindow::on_actionSave_triggered()
{
    QString path = QFileDialog::getSaveFileName(this, tr("Save File"),
    "",tr("Images (*.png *.xpm *.jpg)"));
    QImage image(QSize(500,500),QImage::Format_RGB32);
    QPainter painter(&image);
    scene->render(&painter);
    image.save(path);
}
这是我的源码
我不太懂 RGB32格式,你的意思需要换个格式 让它能透明?

#11


引用 9 楼 dbzhang800 的回复:
http://www.beiww.com/doc/oss/smart-questions.html


汗~

#12


解决了 重新设置下背景色

#13


引用 10 楼 soul_ 的回复:
void MainWindow::on_actionSave_triggered()
{
    QString path = QFileDialog::getSaveFileName(this, tr("Save File"),
    "",tr("Images (*.png *.xpm *.jpg)"));
    QImage image(QSize(500,500),QImag……

哦,那就是你没有设置scene的 setBackgroundBrush,设置为白色就行了。
或者绘图前,将图片填充为白色 image.fill(0xffffffff)

#14


顶,,继续学习

#1


void QGraphicsScene::render(QPainter *painter, const QRectF &target, const QRectF &source,Qt::AspectRatioMode aspectRatioMode)

#2


引用楼主 soul_ 的回复:
~我做了画板程序,现在还剩个保存文件的功能没做,没找到什么好的办法,求大家帮助~
画板程序是用QGraphicsScene做的,注意不是用QPainter(网上有使用QPainter的时候,保存文件的方法)
现在想将画好的图像保存起来,在使用QGraphicsScene的情况下,有什么好的方法吗?找了很久也没太好的API

如果你不介意使用 QPainter,问题倒很简单。

 楼上已经给出关键函数了,我给个具体例子吧:
        QImage image(mysize,QImage::Format_RGB32);
        QPainter painter(&image);
        myscene->render(&painter);
        image.save(mypath/myimagefile.png);

如果你坚持不用 QPainter,真的挺难办。(我想很多人可能和我一样,想知道你不用QPainter的理由)

#3


引用 1 楼 yanj20 的回复:
void QGraphicsScene::render(QPainter *painter, const QRectF &target, const QRectF &source,Qt::AspectRatioMode aspectRatioMode)

不是太会用这个,试了一下保存下来的图片是全黑的
代码我是这样写的:
    QString path = QFileDialog::getSaveFileName(this, tr("Save File"),
    "",tr("Images (*.png *.xpm *.jpg)"));
    QPixmap pix(1000,1000);
    QPainter p(&pix);
    scene->render(&p);
    p.end();
    pix.save(path);
结果不对。当前scene已经添加完其他item了

#4


vb dsaf asf 

#5


引用 2 楼 dbzhang800 的回复:
引用楼主 soul_ 的回复:
~我做了画板程序,现在还剩个保存文件的功能没做,没找到什么好的办法,求大家帮助~
画板程序是用QGraphicsScene做的,注意不是用QPainter(网上有使用QPainter的时候,保存文件的方法)
现在想将画好的图像保存起来,在使用QGraphicsScene的情况下,有什么好的方法吗?找了很久也没太好的API

如果你不介意使用 QPaint……


我在做个roadmap练习,指定了要用GraphicsView框架 来写这个画板

#6


引用 2 楼 dbzhang800 的回复:
如果你不介意使用 QPainter,问题倒很简单。

 楼上已经给出关键函数了,我给个具体例子吧:
  QImage image(mysize,QImage::Format_RGB32);
  QPainter painter(&image);
  myscene->render(&painter);
  image.save(mypath/myimagefile.png);

如果你坚持不用 QPainter,真的挺难办。(我想很多人可能和我一样,想知道你不用QPainter的理由)


同样用QImage也是全黑的 没有显示scene上的图像= =!

#7


是否应该将myscene->render(&painter);这句话放在scene添加item之前(就是开始画图的时候)?

#8


引用 6 楼 soul_ 的回复:
引用 2 楼 dbzhang800 的回复:

如果你不介意使用 QPainter,问题倒很简单。

楼上已经给出关键函数了,我给个具体例子吧:
QImage image(mysize,QImage::Format_RGB32);
QPainter painter(&image);
myscene->render(&painter);
image.save(myp……

你确保细节我和贴的一样? 尽管我保存图片时加了.png(支持透明度),但QImage的格式选择的 RGB32(不支持透明度)

#9


http://www.beiww.com/doc/oss/smart-questions.html

#10


void MainWindow::on_actionSave_triggered()
{
    QString path = QFileDialog::getSaveFileName(this, tr("Save File"),
    "",tr("Images (*.png *.xpm *.jpg)"));
    QImage image(QSize(500,500),QImage::Format_RGB32);
    QPainter painter(&image);
    scene->render(&painter);
    image.save(path);
}
这是我的源码
我不太懂 RGB32格式,你的意思需要换个格式 让它能透明?

#11


引用 9 楼 dbzhang800 的回复:
http://www.beiww.com/doc/oss/smart-questions.html


汗~

#12


解决了 重新设置下背景色

#13


引用 10 楼 soul_ 的回复:
void MainWindow::on_actionSave_triggered()
{
    QString path = QFileDialog::getSaveFileName(this, tr("Save File"),
    "",tr("Images (*.png *.xpm *.jpg)"));
    QImage image(QSize(500,500),QImag……

哦,那就是你没有设置scene的 setBackgroundBrush,设置为白色就行了。
或者绘图前,将图片填充为白色 image.fill(0xffffffff)

#14


顶,,继续学习