Reputation: 5
I have some troubles with image saving. I have to crop "1.png" by rect and save it to file, but an empty one is appearing (0 bytes). What am I doing wrong?
void RedactorForm::cropButtonSlot(int x1, int y1, int x2, int y2) {
QImage pixmap("1.png");
QRect rect(x1,y1,x2,y2);
pixmap=pixmap.copy(rect);
QString fileName("D:/yourFile.png");
QFile file(fileName);
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
pixmap.save(fileName,0,100);
out <<pixmap;
}
Upvotes: 0
Views: 4705
Reputation: 311
I think you have to close the file which you opened before. Also you don't need to open the file at all. You can do that:
QRect rect(x1,y1,x2,y2);
QImage pixmap(x2-x1,y2-y1,QImage::Format_ARGB32);
pixmap.copy(rect);
QFile file("D:/yourFile.png");
pixmap.save(file.fileName(),"PNG");
Upvotes: 0
Reputation: 4010
You don't need use QDataStream
for this task. Use directly save
method of QImage
. Your code should be like this:
QImage pixmap("1.png");
...................
QString fileName("D:/yourFile.png");
QFile file(fileName);
if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
pixmap.save(&file, "PNG");
}
else {
qDebug() << "Can't open file: " << fileName;
}
Upvotes: 0
Reputation: 2666
QImage's save method does not take a file name as a parameter, it taske a QFile. Try this;
pixmap.save(&file, "PNG");
Upvotes: 1