Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Problem with QPixmap scaled
QtWS25 Last Chance

[SOLVED] Problem with QPixmap scaled

Scheduled Pinned Locked Moved General and Desktop
qpixmapscale
5 Posts 2 Posters 10.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fermatqt
    wrote on 5 Sept 2015, 09:57 last edited by fermatqt 9 May 2015, 16:27
    #1

    Hello!
    I'm trying to resize an image with QPixmap.
    This is the code:

    void ImageResize::resize(QString inputImage, QString dir, int width, int height)
    {
        QFileInfo f(inputImage);
        QPixmap pixmap(f.filePath());
        pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        QFile file(dir + f.fileName());
        file.open(QIODevice::WriteOnly);
        pixmap.save(&file, "jpg", 100);
        file.close();
    }
    

    The problem is that the images are copied to the new destination without being resized.
    Where am I wrong?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 5 Sept 2015, 11:20 last edited by
      #2

      @fermatqt said:
      pixmap.scaled returns the scaled bitmap and do not modify the given one, as far as I can remember :)

      so try with
      pixmap = pixmap.scaled(...)

      F 1 Reply Last reply 5 Sept 2015, 12:26
      0
      • M mrjj
        5 Sept 2015, 11:20

        @fermatqt said:
        pixmap.scaled returns the scaled bitmap and do not modify the given one, as far as I can remember :)

        so try with
        pixmap = pixmap.scaled(...)

        F Offline
        F Offline
        fermatqt
        wrote on 5 Sept 2015, 12:26 last edited by
        #3

        @mrjj I tried as you said, but in this case the images are corrupted (zero bytes).

        void ImageResize::resize(QString inputImage, QString dir, int width, int height)
        {
            QFileInfo f(inputImage);
            QPixmap pixmap(f.filePath());
            pixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
            QFile file(dir + f.fileName());
            file.open(QIODevice::WriteOnly);
            pixmap.save(&file, "jpg", 100);
            file.close();
        }
        
        1 Reply Last reply
        0
        • F Offline
          F Offline
          fermatqt
          wrote on 5 Sept 2015, 13:01 last edited by
          #4

          The problem occurs because put width or height to zero.
          So I did the following:

          void ImageResize::resize(QString inputImage, QString dir, int width, int height)
          {
              QFileInfo f(inputImage);
              QPixmap pixmap(inputImage);
              QPixmap newPixmap;
              if(width == 0)
              {
                  newPixmap = pixmap.scaledToHeight(height, Qt::SmoothTransformation);
              }
              else if(height == 0)
              {
                  newPixmap = pixmap.scaledToWidth(width, Qt::SmoothTransformation);
              }
              else
              {
                  newPixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
              }
              QFile file(dir + f.fileName());
              file.open(QIODevice::WriteOnly);
              newPixmap.save(&file, "JPG", 100);
              file.close();
          }
          

          It seems to work.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 5 Sept 2015, 13:25 last edited by
            #5

            ah ok.
            Good to know that it will (try) to scale an image to 0,0 :)

            Remember to flag as solved if your happy.

            1 Reply Last reply
            0

            2/5

            5 Sept 2015, 11:20

            • Login

            • Login or register to search.
            2 out of 5
            • First post
              2/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved