Set DPI for QPixmap?



  • 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.



  • 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 :)


  • Moderators

    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 );
    


  • @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.



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.