PDA

View Full Version : How to copy pixmap and keep scale ratio?



Teerayoot
11th May 2007, 11:51
I need to copy portion of pixmap and fill into onscreen painter.

my code that not work

painter.drawPixmap(pixMap->rect(),*pixMap,recvMap);

the problem is image on screen is not correctly scale like original one
seem it ratio is enlarge to fit target rect.

I just need raw portion of pixmap and scale that keep image ratio.

Thank.

marcel
11th May 2007, 11:59
Use QPixmap::scaled() with Qt::KeepAspectRatio

Teerayoot
11th May 2007, 12:42
I tried but seem not got result i expected.

Portion of image not scale to full screen render area.


I copy portion of pixmap into another pixmap buffer
then make it scale

look at my code


QPixmap buffer(pixMap->rect().width(),pixMap->height() );
QPainter painterBuffer(&buffer);
QPainter painter(this);

//painter.scale(mapScale,mapScale);

int cX = pixMap->width()/2;
int cZ = pixMap->height()/2;


//myPlayer.curPos.x = myX;
//myPlayer.curPos.z = myZ;
int cMapX = pixMap->width()/2;
int cMapY = pixMap->height()/2;



int gamePointX=0;
int gamePointZ=0;

QRect recvMap;
recvMap.setLeft( gamePointX + cMapX + (myX - cX/mapScale)/scale );
recvMap.setRight( gamePointX + cMapX + (myX + cX/mapScale)/scale);
recvMap.setTop( gamePointZ + cMapY - (myZ + cZ/mapScale)/scale);
recvMap.setBottom( gamePointZ + cMapY - (myZ - cZ/mapScale)/scale);

painter.setRenderHint(QPainter::Antialiasing,true) ;
QRectF srcRect=pixMap->rect();

painter.fillRect(rect(),QBrush(Qt::black));



painterBuffer.drawPixmap(QPointF(1,1),*pixMap,recv Map);

buffer.scaled(512,512,Qt::KeepAspectRatio);
painter.save();

painter.drawPixmap(QPointF(1,1),buffer,buffer.rect ());

painter.restore();

painter.setPen(Qt::white);

QFont font(font());
font.setPointSize(24);
painter.setFont(font);

QFontMetrics fm(painter.font());
int fontHeigh = fm.height();
QString tmp;
tmp.sprintf("%.0f %.0f",mouseMovePos.x(),mouseMovePos.y());
//painter.drawText(0,fontHeigh,tr("%1,%2").arg(mouseMovePos.x()).arg(mouseMovePos.y()));
painter.drawText(0,fontHeigh,tmp);
tmp.clear();
tmp.sprintf("My Pos %d %d",myX,myZ);
painter.drawText(0,fontHeigh*2,tmp);
result image