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. Set DPI for QPixmap?

Set DPI for QPixmap?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 5.1k Views 2 Watching
  • 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.
  • c.savurC Offline
    c.savurC Offline
    c.savur
    wrote on last edited by
    #1

    Hello,

    I have an image that has different DPI value in X, Y direction. I am loading the image using QImage then use QPixmap::fromimage(). it shows the image stretched. So I need to display image correctly,

    When I look at the QImage methods, there are two methods setDotsPerMeterX(int) and setDotsPerMeterY(int). Although I set these value, it still stretched the image.

    Do you have any suggestion?

    thank you in advance.

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by Paul Colby
      #2

      Hi @c.savur,

      When I look at the QImage methods, there are two methods setDotsPerMeterX(int) and setDotsPerMeterY(int). Although I set these value, it still stretched the image.

      Just looking at the docs for QImage::setDotsPerMeterX, it says:

      ... determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.

      Since QPixmap does not inherited from QPainter, I guess that means QImage::setDotsPerMeterX won't work for you.

      Do you have any suggestion?

      Not sure. But I guess either:

      1. use QPainter instead (no idea if that's feasible for your use case); or
      2. scale the QImage prior to creating the QPixmap from it, perhaps via QImage::scaled? Something like:
      const double dpiRatio = (double)image.dotsPerMeterX() / (double)image.dotsPerMeterY();
      QPixmap pixmap = QPixmap::fromImage(
          image.scaled(
              (dpiRatio > 1.0) ? image.width() : (int)(image.width() * dpiRatio),
              (dpiRatio < 1.0) ? image.height() : (int)(image.height() / dpiRatio),
              Qt::IgnoreAspectRatio
          )
      );
      

      Just an idea (don't know if it'll work or not). Good luck :)

      c.savurC 1 Reply Last reply
      1
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        what happens if you do the following?

        QImage img(...);  // your image
        QPixmap pix( image.size() );  // not sure if this size is correct of if it has to be recalculated considering the dpi ratio
        QPainter p(&pix);
        p.drawImage( 0, 0, img );
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        c.savurC 1 Reply Last reply
        0
        • Paul ColbyP Paul Colby

          Hi @c.savur,

          When I look at the QImage methods, there are two methods setDotsPerMeterX(int) and setDotsPerMeterY(int). Although I set these value, it still stretched the image.

          Just looking at the docs for QImage::setDotsPerMeterX, it says:

          ... determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.

          Since QPixmap does not inherited from QPainter, I guess that means QImage::setDotsPerMeterX won't work for you.

          Do you have any suggestion?

          Not sure. But I guess either:

          1. use QPainter instead (no idea if that's feasible for your use case); or
          2. scale the QImage prior to creating the QPixmap from it, perhaps via QImage::scaled? Something like:
          const double dpiRatio = (double)image.dotsPerMeterX() / (double)image.dotsPerMeterY();
          QPixmap pixmap = QPixmap::fromImage(
              image.scaled(
                  (dpiRatio > 1.0) ? image.width() : (int)(image.width() * dpiRatio),
                  (dpiRatio < 1.0) ? image.height() : (int)(image.height() / dpiRatio),
                  Qt::IgnoreAspectRatio
              )
          );
          

          Just an idea (don't know if it'll work or not). Good luck :)

          c.savurC Offline
          c.savurC Offline
          c.savur
          wrote on last edited by
          #4

          @Paul-Colby said:

          Hi @c.savur,

          When I look at the QImage methods, there are two methods setDotsPerMeterX(int) and setDotsPerMeterY(int). Although I set these value, it still stretched the image.

          Just looking at the docs for QImage::setDotsPerMeterX, it says:

          ... determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.

          Since QPixmap does not inherited from QPainter, I guess that means QImage::setDotsPerMeterX won't work for you.

          Yes, I read this part, so I try to use QPainter to drawImage on QPixmap but it did not work.

          Do you have any suggestion?

          Not sure. But I guess either:

          1. use QPainter instead (no idea if that's feasible for your use case); or
          2. scale the QImage prior to creating the QPixmap from it, perhaps via QImage::scaled? Something like:
          const double dpiRatio = (double)image.dotsPerMeterX() / (double)image.dotsPerMeterY();
          QPixmap pixmap = QPixmap::fromImage(
              image.scaled(
                  (dpiRatio > 1.0) ? image.width() : (int)(image.width() * dpiRatio),
                  (dpiRatio < 1.0) ? image.height() : (int)(image.height() / dpiRatio),
                  Qt::IgnoreAspectRatio
              )
          );
          

          Just an idea (don't know if it'll work or not). Good luck :)

          Thank you, Actually, it works with small changes :) Final code is below. When scale operation using fast transformation, it gives me a black image. After I changed it to smooth transformation, I was able to get scaled image.

              const double dpiRatio = (double)image.dotsPerMeterY() / (double)image.dotsPerMeterX();
              QPixmap pixmap = QPixmap::fromImage(
                  image.scaled(
                      (dpiRatio > 1.0) ? image.width() : (int)(image.width() * dpiRatio),
                      (dpiRatio < 1.0) ? image.height() : (int)(image.height() / dpiRatio),
                      Qt::IgnoreAspectRatio, Qt::SmoothTransformation
                  )
              );
          

          Thanks for the help.

          1 Reply Last reply
          0
          • raven-worxR raven-worx

            what happens if you do the following?

            QImage img(...);  // your image
            QPixmap pix( image.size() );  // not sure if this size is correct of if it has to be recalculated considering the dpi ratio
            QPainter p(&pix);
            p.drawImage( 0, 0, img );
            
            c.savurC Offline
            c.savurC Offline
            c.savur
            wrote on last edited by
            #5

            @raven-worx

            I tried but I could not make it worked. I was crashing, before calling drawImage I was setting, setDotsPerMeterX and Y().

            I have tried this approach too. link

            Thank you.

            1 Reply Last reply
            0

            • Login

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