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。


你可能感兴趣的:(Qt开发)