PDA

View Full Version : Add letterbox to resized QPixmap



Jay-D
2nd April 2013, 12:09
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

ChrisW67
3rd April 2013, 05:31
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.


QPixmap source("test.png"); // say 400x300
QPixmap dest(512, 512);
dest.fill(Qt::black);
QPixmap resized = source.scaled(dest.size(), Qt::KeepAspectRatio);
QPainter p(&dest);
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");

Jay-D
6th April 2013, 04:52
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.