Qt图片按原比例缩放


1.选择图片

QString strFilePath = QFileDialog::getOpenFileName(this, tr("Select file"), QStandardPaths::standardLocations(QStandardPaths::PicturesLocation), "*.png *.jpg");
if (strFilePath.isEmpty())
{
  return;
}

2.图片合法性校验

QImageReader imgReader;
imgReader.setFileName(strFilePath);
if (!imgReader.canRead())
{
    QMessageBox::information(0, "", QObject::tr("The image data is corrupt, Please select a valid one!"));
    return;
}

3.原图比例缩放

int nThumbnailWidth  = 80;
int nThumbnailHeight = 80;
QLable *pThumbLab = new QLabel(this);
pThumbLab->setFixedSize(nThumbnailWidth, nThumbnailHeight);

QSize sizeImage = imgReader.size();
QPixmap pixThumb(mstrPicPath);
if (sizeImage.width() > nThumbnailWidth || sizeImage.height() > nThumbnailHeight)
{
    if (sizeImage.width() > sizeImage.height())
    {
        pixThumb = pixThumb.scaledToWidth(nThumbnailWidth, Qt::SmoothTransformation);
    }
    else
    {
        pixThumb = pixThumb.scaledToHeight(nThumbnailHeight, Qt::SmoothTransformation);
    }
    pThumbLab->setFixedSize(pixThumb.size());
}
else
{
    pThumbLab->setFixedSize(sizeImage);
}
pThumbLab->setPixmap(pixThumb);

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM