1. 程式人生 > >Qt開啟,儲存,另存為圖片

Qt開啟,儲存,另存為圖片

Qt開啟一張圖片並顯示在QLabel控制元件中:

    QString filename=QFileDialog::getOpenFileName(this,tr("選擇影象"),"",tr("Images (*.png *.bmp *.jpg)"));
    if(filename.isEmpty())
        return;
    else
    {
        QImage img;
        if(!(img.load(filename))) //載入影象
        {
            QMessageBox::information(this, tr("開啟影象失敗"),tr("開啟影象失敗!"));
            return;
        }
        ui->label->setPixmap(QPixmap::fromImage(img.scaled(ui->label->size())));
    }

Qt擷取QLabel控制元件上的內容,並另存為:
    QString filename1 = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.png *.bmp *.jpg)")); //選擇路徑
    QScreen *screen = QGuiApplication::primaryScreen();
    screen->grabWindow(ui->label->winId()).save(filename1);
Qt利用OpenCV的imwrite函式另存為圖片:
    Mat img = imread("D:/1.jpg",1);
    filename = QFileDialog::getSaveFileName(this,tr("Save Image"),"",tr("Images (*.png *.bmp *.jpg)")); //選擇路徑
    string fileAsSave = filename.toStdString();
    imwrite(fileAsSave,img);
另存為時,圖片的格式寫全,比如:寫成img.jpg而不是img。