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.
QRect is wrong
It's not wrong per se,
QRectis 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.
But how i can move my cropping rectangle from one coordinate system to another? (cropped area will have it's own coordinate system).
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());