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