PDA

View Full Version : Saving already opened image ...



Godlike
28th March 2006, 16:30
I made a simple image viewer, now how do i save that image with save and also save as ? Can you paste me some code example how to do that.

I have read the image with:


QString fileName = QFileDialog::getOpenFileName(this,tr("Open File"), QDir::currentPath());
if (!fileName.isEmpty())
{
QImage image(fileName);
if (image.isNull())
{
QMessageBox::information(this, tr("ImageViewer"), tr("Cannot load %1.").arg(fileName));
return;
}
imageLabel->setPixmap(QPixmap::fromImage(image));


thx.

jpn
28th March 2006, 16:40
For saving as, check out: QFileDialog::getSaveFileName() (http://doc.trolltech.com/4.1/qfiledialog.html#getSaveFileName)
For saving QImage to a file: QImage::save() (http://doc.trolltech.com/4.1/qimage.html#save)

Godlike
28th March 2006, 18:58
Well yes, this opens me the dialog to enter filename. But how do i actualy get my image to save to that filename ?

Godlike
28th March 2006, 19:16
To be more exact, done this:


void ImageViewer::save()
{
QString s = QFileDialog::getSaveFileName(this,"Choose a filename to save under",QDir::currentPath(),"Images (*.png *.xpm *.jpg)");
QImage image(s);
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPG");
}

doesnt save nothing as i specify :)
What have i missunderstood ? :)

jpn
28th March 2006, 19:21
This is something I wrote a while back, maybe you'll find it usable


void ImageViewer::saveFileAs()
{
// construct a filter of all supported formats
QString filter;
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
foreach (QString format, formats)
{
filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
}

// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}

// get save file name
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this,
"Save image as", path, filter, &selectedFilter);

if (!fileName.isEmpty())
{
// keep track of the current path
path = QDir(fileName).path();

// check for the selected format
QString format = selectedFilter.split(" ").at(0);
QFileInfo fi(fileName);
if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
fileName.chop(fi.suffix().length());

// set correct suffix
fileName += "." + format.toLower();
}

// save image in the selected format
if (!image.save(fileName, format.toAscii().constData()))
{
QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(fileName));
}
}
}

jpn
28th March 2006, 19:23
doesnt save nothing as i specify :)
What have i missunderstood ? :)
Assuming that you want to save the shown image, put the "QImage image" as a member variable and use that same variable for loading and saving.

Godlike
28th March 2006, 20:31
Realy a newbie at this but i cant get it to work, thanx for sharing this code first. I made it this far, i always get unable to save image (path is ok to where it wants to save and extension). My code till now:


void ImageViewer::save()
{
// construct a filter of all supported formats
QString filter;
QImage image;
QList<QByteArray> formats = QImageWriter::supportedImageFormats();
foreach (QString format, formats)
{
filter += QString("%1 files (*.%2);;").arg(format.toUpper()).arg(format);
}

// remove unnecessary chars from the end of the filter
if (filter.endsWith(";;"))
{
filter.chop(2);
}

// get save file name
QString selectedFilter;
QString s = QFileDialog::getSaveFileName(this, "Save image as", QDir::currentPath(), filter, &selectedFilter);

if (!s.isEmpty())
{

// check for the selected format
QString format = selectedFilter.split(" ").at(0);
QFileInfo fi(s);

if (!fi.suffix().endsWith(format, Qt::CaseInsensitive))
{
// remove possible incorrect suffix
s.chop(fi.suffix().length());
// set correct suffix
s += "." + format.toLower();
}

// save image in the selected format
if (!image.save(s, format.toAscii().constData()))
{
QMessageBox::information(this, "Image Viewer", QString("Unable to save %1.").arg(s));
}
}
}

wysota
28th March 2006, 20:37
I think you should actually use QFile if you want to write an image to a file instead of QBuffer.

The simplest way to save a QImage to a file should be something like this:


QImage img;
//...
QString filename = QFileDialog::getSaveFileName(this, "Choose file");
if(!filename.isNull()) img.save(filename, "PNG");

A more complicated but more flexible way:


QImage img;
//...
QString filename = QFileDialog::getSaveFileName(this, "Choose file");
if(!filename.isNull()){
QFile file(filename);
if(file.open(QIODevice::WriteOnly)){
image.save(&file, "PNG");
}
}

Also remember to use "JPEG" not "JPG" for jpeg files.

Godlike
28th March 2006, 20:42
This makes file great, but its empty ? All black and not saved what i have opened. You can see code how i opened file in my first post.

thx.

jpn
28th March 2006, 20:54
As I said, make the QImage image member variable.
If you instantiate a new QImage as a local variable in the function where you save, it's not the same image than you are viewing!

Then change the line 4 in your first post from:

QImage image(fileName);
to

image.load(fileName);

Load to and save the same image.

Godlike
28th March 2006, 21:14
imageviewer.cpp:29: error: ‘image’ was not declared in this scope
imageviewer.cpp:35: error: no matching function for call to ‘QPixmap::fromImage(<type error>)’
/usr/include/QtGui/qpixmap.h:105: note: candidates are: static QPixmap QPixmap::fromImage(const QImage&, Qt::ImageConversionFlags)
make: *** [imageviewer.o] Error 1

if i change line 4 to what u told me :)

Godlike
28th March 2006, 21:19
Oh and i would need to get that string fileName which is a name of that image then too to be visible in save function.

I just dont get it, simple as that :o

jpn
28th March 2006, 21:36
imageviewer.cpp:29: error: ‘image’ was not declared in this scope
imageviewer.cpp:35: error: no matching function for call to ‘QPixmap::fromImage(<type error>)’
/usr/include/QtGui/qpixmap.h:105: note: candidates are: static QPixmap QPixmap::fromImage(const QImage&, Qt::ImageConversionFlags)
make: *** [imageviewer.o] Error 1

if i change line 4 to what u told me :)

Did you declare a QImage member variable?

in imageviewer.h:


class ImageViewer ...
...
private:
QImage image;
..


Edit: and the same thing goes for the QString fileName.. You know how to declare member variables, and what they are for, right? ;)

Godlike
28th March 2006, 21:47
Ups you got me big time. Dunno why exactly needs to be there :p

But i put it there (probably wrong again :D ), and i get this error ... again :cool:

imageviewer.h:38: error: field ‘image’ has incomplete type

i put it in imageviewer.h so it is private and like this:

class ImageViewer : public QMainWindow
{
...
private:
QImage image;
QString fileName;
};

Please dont hate me ;)

Godlike
28th March 2006, 22:17
Forget it was a stupid include missing ;) damn i forgot how to code it seems :D

And the silly thing works now ofcourse :) thanx for everything