Copy rotated part of QImage



  • Hello, i have a QGraphicsScene with a QGraphicsPixmapItem as a background and custom rotateble QGraphicsItem as a frame. I need to cut an area from the background and save it as a QImage. So far, i've tried to get mapToScene(boundingRect()) coordinates of QGraphicsItem and cut resulting Rect from QGraphicsPixmapItem->pixmap()->toImage(). It doesn't because QRect is wrong.
    alt text


  • Qt Champions 2016

    @dream_captain said in Copy rotated part of QImage:

    QRect is wrong

    It's not wrong per se, QRect is a normal rectangle, i.e. aligned with the axes, so it can't be used to represent a rotated rectangle. What you can do, is for example to cut the bounding rect, rotate the image to the proper angle, and then cut a second time. Finally rotate back to obtain the correct orientation. This however will not come without cost - when rotating raster data you will lose some fidelity in the process (as pixels are always aligned with the axes).
    Another possibility is to write your own copy() function that accepts a QPolygon, but this is going to require some more math to calculate which pixels fall into the polygon and which do not.

    Kind regards.



  • @kshegunov Thanks!
    But how i can move my cropping rectangle from one coordinate system to another? (cropped area will have it's own coordinate system).


  • Qt Champions 2016

    @dream_captain said in Copy rotated part of QImage:

    But how i can move my cropping rectangle from one coordinate system to another?

    Well, my point was that you can't transform the rectangle itself, so you'd do something like this:

    QImage background;
    QTransform transform;
    QRect clipRectangle;
    
    QImage clippedImage = background.transformed(transform).copy(clipRectangle).transformed(transform.inverted());
    

Log in to reply
 

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