PDA

View Full Version : Can't save QImage



hvengel
16th January 2008, 01:25
I am creating a QImage and I would like to save it to disk. But the QImage::save() always return false and no image is saved. The image is valid since I am displaying it in my Widget at the time I try to save it. My code looks like this:


QPainter painter(this);

QImage backGround;

painter.setRedirected (this, &backGround, QPoint(0, 0));

painter.setWindow( 0, 0, pixcols - 1, pixrows - 1);

// bunch of painter commands

painter.setRedirected (&backGround, this, QPoint(0, 0));

QString file = "image.png";
if(backGround.save(file, "PGN", -1))
qDebug("it worked");
else
qDebug("it didn't work");

painter.setWindow( 0, 0, pixcols - 1, pixrows - 1);


QRect rect(0, 0, pixcols - 1, pixrows - 1);

painter.drawImage(rect, backGround, rect);

I have tried all kinds of variations with different image types (pgn, PNG, jpg, JPG, tif, TIF....) and nothing works. What am I doing wrong?

aamer4yu
16th January 2008, 04:00
did u try giving the name as "image" and not as "image.png" ??
am not sure if this is the problem ...

ChristianEhrlicher
16th January 2008, 06:41
Your create an empty (null) image - how should Qt save this image?

hvengel
16th January 2008, 22:51
I had not tried leaving off the file extension. But I just tested it and it did not change the behavior.

Yes it was NULL. So I fixed that by declaring the QImage like this:

QImage backGround(pixcols, pixrows, QImage::Format_RGB32);

wysota
16th January 2008, 23:06
What is the format "PGN"? Shouldn't it be "PNG"?

David Burnett
13th June 2008, 20:35
I am creating a QImage and I would like to save it to disk. But the QImage::save() always return false and no image is saved. The image is valid since I am displaying it in my Widget at the time I try to save it. My code looks like this:


Hi, did you ever solve this problem, I appear to be having the same issue



for (int i = 0; i < QImageWriter::supportedImageFormats().size(); ++i) {
std::cout << QImageWriter::supportedImageFormats().at(i).constD ata() << std::endl;
}
uchar *buffer = (uchar *)malloc(32 * 32 * 3);

QImage image = QImage (buffer, 32, 32, 32 * 3, QImage::Format_RGB32);
QString str = "/Users/vargol/Source/FractGo/test.bmp";

bool saved = image.save(str);

std::cout << "Saved image " << saved << std::endl;





bmp
ico
jpeg
jpg
png
ppm
tif
tiff
xbm
xpm
Saved image 0


I've tried different suffixes, using a format string, everything I can think off..

David Burnett
14th June 2008, 21:07
Fixed it myself saw the RGB, didn't see the 32...d'oh.