質問

when i resize a QPixmap with scaledToHeight and then transform it to an QByteArray, the size of this ByteArray is exactly the size of ByteArray from the unscaled QPixmap. Is there a possibility to shrink QPixmap in pixel-size and file-size?

Best regards

役に立ちましたか?

解決

Here is a proof-of-concept that shows that what you are doing is certainly possible. Maybe you can see what you are doing different?

#include <QtGui>

int main(int argc, char **argv) {
  QApplication app(argc, argv);

  QPixmap pixmap("/path/to/image.jpg");
  QByteArray bytes1;
  QBuffer buffer1(&bytes1);
  buffer1.open(QIODevice::WriteOnly);
  pixmap.save(&buffer1, "png");
  qDebug() << bytes1.size();

  pixmap = pixmap.scaledToHeight(100);
  QByteArray bytes2;
  QBuffer buffer2(&bytes2);
  buffer2.open(QIODevice::WriteOnly);
  pixmap.save(&buffer2, "png");
  qDebug() << bytes2.size();

  return app.exec();
}

Two guesses:

  • scaledToHeight returns a scaled copy of the original image. It does not scale the pixmap instance directly.
  • If you are reusing the same QByteArray, you may have to truncate it before seeing the difference in size. That is, the capacity of the QByteArray may remain larger even through the actual content is much smaller.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top