How to resize QImage



  • Hi:

    To resize QPixmap 'pmap' I can use
    @
    void resizePmap(QPixmap &pmap)
    {
    pmap = QPixmap(newSize); // old pmap's memory will be recycled by Qt. See my previous post.
    }
    @
    As for QImage,
    @
    void resizeImage(QImage &img)
    {
    img = new QImage(newSize); // Will the old memory of img get recycled by Qt too?

    }
    @

    Thanks!

    -Todd
    Edit: Please use code tags (@) around your code sections; Andre



  • That doesn't even compile. If you want to create a brand new image with a different size, then
    @
    QImage image(oldSize);
    /* ... */
    image = QImage(newSize);
    @

    works. That's not resizing (QImage::scaled).



  • Sorry typo
    @
    QImage img(oldSize);

    void resizeImage(QImage &img)
    {
    img = QImage(newSize); // Will the old memory of img get recycled by Qt too?
    }
    @

    My question is : Is the oldSize chunk of memory going to be recycled by Qt?
    Or I have to take care of it?

    Thanks!
    -Todd

    Edit: please use @ tags around code sections; Andre



  • Or you can use method scaled(size); which will return new image scaled to size



  • By resizing, I assume you will want to keep some of the pixmap / image data from the original pixmap / image? I'm quite sure your example code will not do that - you are just creating a new image with a different size and storing it in the same variable - the contents will be undefined. Or am I missing something? (Your comment that the memory will be recycled makes no sense to me.)

    If you have a pixmap / image, and want to resize it, you have to create a copy of that pixmap / image that contains a part of that image, or a scaled version of that image. To do this, you use the copy() or scaled() methods, respectively. E.g.:

    @
    pmap = pmap.copy(x, y, w, h);
    pmap = pmap.scaled(w, h);
    image = image.copy(x, y, w, h);
    image = image.scaled(w, h);
    @

    scaled() also takes additional arguments that deal with aspect ratio and transformation mode, have a look at the docs for QImage and QPixmap to see how to use them.



  • [quote author="tclin1998" date="1305739253"]
    My question is : Is the oldSize chunk of memory going to be recycled by Qt?
    Or I have to take care of it?
    [/quote]

    QImage uses "implicit data sharing":http://doc.qt.nokia.com/latest/implicit-sharing.html#implicit-data-sharing, so you don`t have to take care of it.


Log in to reply
 

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