怎样用qt代码编写图片保存格式?

怎样用qt代码编写图片保存格式?,第1张

qt提供了多个保存图片的接口,比较常用的接口如下

bool QPixmap::save ( const QString &fileName, const char * format = 0, int quality = -1 ) const

参数说明:

fileName 文件的路径

可选参数:format 图片的格式(qt目前支持的格式见下表),如果未设置该值

则根据文件路径的后缀名来判断图片的存储格式。

可选参数:quality 图片的质量(可设置成0-100之内的值,数值越大说明保存的质量越好),如果未设置该值,则按照默认的设置来保存图片。

表一:qt 4.6.3支持的文件格式

格式 描述

BMP Windows Bitmap

JPG Joint Photographic Experts Group

JPEG Joint Photographic Experts Group

PNG Portable Network Graphics

PPM Portable Pixmap

TIFF Tagged Image File Format

XBM X11 Bitmap

XPM X11 Pixmap

bool QImage::save ( const QString &fileName, const char * format = 0, int quality = -1 ) const

参数说明同上

这里举个使用QPixmap保存图片的例子:

QPixmap pixmap

if(pixmap.load("D:\\images\\source.png"))

{

if(pixmap.save("distance.jpg"))

{

//save image successful

}else

{

//save image failure

}

}else

{

//load image failure

}

注:如果需要对图片的保存参数进行更多的设置,可以使用QImageWriter提供的接口来实现

如果图片比较小的话,可以在qrc文件里面添加图片的路径和别名

<RCC>

<qresource prefix="/image">

<file alias="check_f">image/Check_f.png</file>

</qresource>

</RCC>

我这个,图片路径是image/Check_f.png,别名是check_f

qt会自动将这些图片编译成2进制文件,和exe一起

调用时,QLabel *l = new QLabel(this)

l->setPixmap(QPixmap(":/image/check_f"))

即可

QString myDirPath = QString::fromLocal8Bit("C:\\documents and settings\\xxxx\\桌面")

QDir myDir = QDir(myDirPath)

myDirPath = myDir.exists() ? myDirPath:QDir::homePath()

m_fileName = QFileDialog::getOpenFileName(this, tr("Select Image:"),

myDirPath,

tr("Images (*.jpg *.png *.tif *.ico *.bmp *.gif)"))

QT支持jpg, png, tif, ico, bmp, gif等等大多数的图片格式,其中部分是以插件的形式支持。

希望对你有所帮助


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/bake/11905334.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存