Results 1 to 12 of 12

Thread: Maintaining Aspect Ratio of Image in Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Post Maintaining Aspect Ratio of Image in Qt

    Dear All,

    Is there any way for maintaining the aspect ratio for the Image being taken from the db.
    What exactly I am doing is I am populating db with the image (the datatype for the image is BLOB in DB) & later on I am displaying that image on the reports.

    The code I have used for displaying the Image on the reports is below,

    Qt Code:
    1. QPainter *reportPainter;
    2. QByteArray myImage;
    3. p.loadFromData(myImage); //myImage = Acquired from the db
    4. p.scaled(120,60,Qt::KeepAspectRatio); //This is not required as x & y co-ordinates are fix
    5. reportPainter->drawImage(QRect(60,0,120,60),p.toImage()); //Adding image in QPainter with rectangle size kept fixed.
    To copy to clipboard, switch view to plain text mode 

    In Line of Code no. 4 : Image is scaled to fix width & height (viz. not the requirement, as it should be calculated based on the acquired image height & width)
    In Line of Code no. 5 : Similarly, the Image is drawn in the fix rectangle.

    How to get the image's height & width present in the db? or Is there any other way to do so?
    And yes I want to maintain the aspect ratio of the image in the report acquired from the database.

    Thanks in Advance.
    Regards,
    Rohit K

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Maintaining Aspect Ratio of Image in Qt

    Line 4 loads them image, it does not scale it at all.
    Line 5 is a very expensive NOOP, you are creating a scaled version of the image and then ignore it (return value not used at all).
    In Line 6 is another unneeded operation, i.e. toImage(). If you want to work with QImage, use QImage instead of QPixmap. Right now you convert from image to pixmap (internally in line 4) and then back again in line 6.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Maintaining Aspect Ratio of Image in Qt

    Quote Originally Posted by anda_skoa View Post
    Line 4 loads them image, it does not scale it at all.
    Yeah I know this.
    Line 5 is a very expensive NOOP, you are creating a scaled version of the image and then ignore it (return value not used at all).
    What is the right way to do it?
    Does storing the returned value really helps?
    In Line 6 is another unneeded operation, i.e. toImage(). If you want to work with QImage, use QImage instead of QPixmap. Right now you convert from image to pixmap (internally in line 4) and then back again in line 6.
    I have used QByteArray to retrieve image from the database (present as BLOB in db) hence, i am not converting the image i am loading the data (present in QByteArray datatype) into the QPixMap.
    I have used QPixMap instead of the QImage because i want to maintain the aspect ratio , as it cannot be done using the QImage (I have tried it before & Image was coming distorted using QImage) & hence, again converting the QPixMap to QImage to display it on the QPainter .

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Maintaining Aspect Ratio of Image in Qt

    Quote Originally Posted by rohitkk View Post
    Does storing the returned value really helps?
    It helps if you want to use the scaled image. If your goal is to use CPU time then you can of course ignore the result.
    However, from your general description it sounds like you want the scaled image, not the original.

    Quote Originally Posted by rohitkk View Post
    I have used QByteArray to retrieve image from the database (present as BLOB in db) hence, i am not converting the image i am loading the data (present in QByteArray datatype) into the QPixMap.
    Loading is always done through QImage, so loading into QPixmap loads into a QImage and then converts it to a QPixmap.

    Quote Originally Posted by rohitkk View Post
    I have used QPixMap instead of the QImage because i want to maintain the aspect ratio
    What?
    Qimage::scaled() takes the same arguments.

    Cheers,
    _

  5. #5
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Maintaining Aspect Ratio of Image in Qt

    Ok .
    I have modified my Code.
    Qt Code:
    1. QImage img;
    2. if (img.loadFromData(schoolLogo)) {
    3. reportPainter->drawImage(QRect(60,0,120,60),img.scaled(img.size(),Qt::KeepAspectRatio));
    4. }
    To copy to clipboard, switch view to plain text mode 

    Please see the screenshot for ,
    screen 1 : my actual Image (.jpg)images (1).jpg
    screen 2: In my pdf that I have generated containing that picture Untitled.jpg

    Hence, in screen 1 viz the actual image , have different height & width.
    whereas in screen 2 viz the image on the pdf , where the aspect ratio is not at all maintained.

    Please help me out to maintain the aspect ratio of the image & why isn't the Qt::KeepAspectRatio not working as desired?

    Thanks in advanced.

    Regards,
    Rohit

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Maintaining Aspect Ratio of Image in Qt

    What do you think this part of the code does?
    Qt Code:
    1. img.scaled(img.size(),Qt::KeepAspectRatio)
    To copy to clipboard, switch view to plain text mode 
    I.e. in words, what do you expect as the return value of img.scaled()?


    Cheers,
    _

  7. #7
    Join Date
    Oct 2009
    Posts
    66
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Maintaining Aspect Ratio of Image in Qt

    Try this as it worked for me:
    Qt Code:
    1. //viewWidth,viewHeight is the size of the image in the aspect ratio needed.
    2. image = im.scaled(viewWidth,viewHeight,Qt::IgnoreAspectRatio,Qt::SmoothTransformation);
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Maintaining Aspect Ratio of Image in Qt

    What do you think this part of the code does?
    Qt Code:
    1. img.scaled(img.size(),Qt::KeepAspectRatio)
    To copy to clipboard, switch view to plain text mode 
    What is the correct way to do it?
    As you know I have fixed size rectangle,
    Qt Code:
    1. QRect(60,0,120,60)
    To copy to clipboard, switch view to plain text mode 
    And I want to display the image in this rectangle with it's aspect ratio maintained.

    I.e. in words, what do you expect as the return value of img.scaled()?
    A scaled Image . Right?

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Maintaining Aspect Ratio of Image in Qt

    Quote Originally Posted by rohitkk View Post
    What is the correct way to do it?
    Correct depends on the desired outcome.
    Your code is correct if you want an expensive copy of the original image.
    Quote Originally Posted by rohitkk View Post
    As you know I have fixed size rectangle,
    Qt Code:
    1. QRect(60,0,120,60)
    To copy to clipboard, switch view to plain text mode 
    Which is why I was wondering why you are not using that size instead of the image's own size.

    Quote Originally Posted by rohitkk View Post
    A scaled Image . Right?
    Yes, scaled to exactly the same size.

    I have troubles understanding why you are not passing the size you want to scale to.


    Cheers,
    _

Similar Threads

  1. QMdiSubWindow maintaining aspect ratio while resizing ?
    By Abhishek Bansal in forum Newbie
    Replies: 0
    Last Post: 9th October 2012, 16:36
  2. Replies: 0
    Last Post: 21st November 2011, 07:55
  3. Printing and Aspect ratio
    By cpt4 in forum Qwt
    Replies: 9
    Last Post: 30th September 2011, 14:28
  4. Replies: 5
    Last Post: 11th June 2011, 15:29
  5. Preserving aspect ratio of image
    By Banjo in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2009, 13:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.