Add letterbox to resized QPixmap
Hi,
I want to resize an image with 4:3 aspect ratio and display it centered in a quadratic QGraphicsPixmapItem, thus having black bars above and below the image (letterbox). I know how to resize the image preserving its aspect ratio, but not how to center it on the PixmapItem and give the PixmapItem a black background or how to paste the resized image on a black quadratic image. Any suggestions?
Thanks in advance,
Jay-D
Re: Add letterbox to resized QPixmap
I have no idea what a "quadratic QGraphicsPixmapItem" or "quadratic image" is, perhaps you meant square. Here is one way to get your letter (or pillar) boxed pixmap.
Code:
QPixmap source
("test.png");
// say 400x300 dest.fill(Qt::black);
QPixmap resized
= source.
scaled(dest.
size(), Qt
::KeepAspectRatio);
if (resized.width() < dest.width())
p.drawPixmap( (dest.width() - resized.width())/2, 0, resized);
else
p.drawPixmap( 0, (dest.height() - resized.height())/2, resized);
p.end();
dest.save("output.png");
Re: Add letterbox to resized QPixmap
Wow, of course I meant "square", must have been the lack of sleep...
Thanks a lot for your answer, I'm slowly starting to see the possibilities of QPainter.