//for starf study #include "mainwindow.h" #include "ui_mainwindow.h" #include
<QDebug> #include<QPainter> #include<QPixmap> #include<QRect> #include<QImage> #
include<QBitmap> #include<QImageReader> #include<QRegion> #include<QTransform>
//QPixmap 类是一种屏幕外图像表示,可以用作绘制设备。 //QPixmap是为在屏幕上显示图像而设计和优化的。
//QPixmap几乎和QImage提供相同的功能 //区别:1、QPixmap依赖于硬件,QImage不依赖 //
2、在除了Windows的其他一些平台上,两种类的底层原理可能不同, // 这会导致计算、绘画修改图片的时候,qimage更快;显示的时候qpixmap更快
// 3、QPixmap不可以在显示线程之外访问,而QImage可以
//用法:当要操作图像对象的时候,就使用QImage,最后渲染的时候,使用QPixmap::convertFromImage
函数转换为QPixmap对象进行渲染 //这里面提到的mask指的是图像蒙版,详情请查询:PS教程 - 蒙版 MainWindow::MainWindow(
QWidget*parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(
this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::paintEvent(
QPaintEvent*) { QPixmap pix = QPixmap(":/new/prefix1/D:/test.png");
//用现成的图片创建一个pix对象 QPainter paint(this); QRect rc = QRect(15,15,200,200);
//paint.drawPixmap(rc,pix); //QPixmap QPixmap::alphaChannel() const //void
QPixmap::setAlphaChannel(const QPixmap &p) //一般不使用的一对方法 qDebug()<< pix.cacheKey(
);//返回标识此QPixmap的数字。 //如果不同的QPixmap对象引用相同的内容,则它们只能具有相同的KEY。
//更改pixmap时,cacheKey()将更改。 paint.drawEllipse(QRect(20,20,15,15)); paint.fillRect
(rc,QColor(0,0,0,0)); QImage img = QImage(150,150,QImage::Format_ARGB32); img.
fill(QColor(0,0,0,0)); qDebug()<<pix.convertFromImage(img);//参数2设置色彩模式
//Qt::AutoColor (默认)-如果图像的深度为1且仅包含黑白像素,则pixmap将变为单色 //Qt::ColorOnly
pixmap会抖动/转换为本机显示深度。 //Qt::MonoOnly pixmap变成单色。 //[static] QPixmap
QPixmap::fromImage(QImage &&image, Qt::ImageConversionFlags flags =
Qt::AutoColor) QPixmap copy = pix.copy(rc); //返回给定矩形指定的像素贴图子集的深度副本。 //QPixmap
QPixmap::copy(int x, int y, int width, int height) const QBitmap mask = pix.
createHeuristicMask(true);//创建并返回此pixmap的遮罩。
//从一个角点选择一种颜色,然后从所有边缘开始去除该颜色的像素。这个函数很慢 QBitmap mask2 = pix.createMaskFromColor(
QColor(0xffff0000), Qt::MaskInColor); //基于给定的颜色创建并返回此pixmap的遮罩 //
Qt::MaskInColor 则与maskColor匹配的所有像素都将是透明的 //Qt::MaskOutColor
与maskColor匹配的所有像素都将不透明。 qDebug()<<pix.defaultDepth(); //返回应用程序使用的默认pixmap深度。
qDebug()<<pix.depth(); //返回此图的深度。 pix.detach();
//从共享的pixmap数据中分离pixmap。这个函数在其他很多函数中被隐式调用 一般不手动调用 qDebug()<<pix.devicePixelRatio
(); //返回pixmap的设备像素比率。 pix.fill(QColor(0,0,0,0)); QImageReader* re = new
QImageReader(); QPixmap reader = pix.fromImageReader(re);
//直接从imageReader读取的图像创建QPixmap
//在某些系统上,直接将图像读取到QPixmap比读取QImage以将其转换为QPixmap使用的内存更少。 qDebug()<<pix.hasAlpha();
//如果此pixmap具有alpha通道或掩码,则返回true,否则返回false。 qDebug()<<pix.hasAlphaChannel();
//如果pixmap的格式符合alpha通道,则返回true,否则返回false。 qDebug()<<pix.height(); //返回pixmap的高度。
qDebug()<<pix.isNull();//如果这是一个空的pixmap,则返回true; qDebug()<<pix.isQBitmap();
//如果这是QBitmap,则返回true;否则返回false。 pix.load(":/new/prefix1/D:/test.png","jpg");
//加载一个图像 //参数2指定格式,如果不指定,那么系统将更具数据头猜测类型 uchar* temp = (uchar *) malloc(10000);
qDebug()<<pix.loadFromData(temp, 10000,"jpg");
//从给定二进制数据中加载一个pixmap。pixmap加载成功,则返回true;
//加载程序尝试使用指定的格式读取pixmap。如果不指定,那么系统将更具数据头猜测类型 //bool QPixmap::loadFromData(const
QByteArray &data, const char *format = nullptr, Qt::ImageConversionFlags flags
= Qt::AutoColor) QBitmap mas = pix.mask(); //从pixmap的alpha通道提取位图遮罩。 qDebug()<<
pix.rect(); //返回像素贴图的封闭矩形。 qDebug()<<pix.save("D:\\testpix.jpg","jpg");
//使用指定的图像文件格式,将pixmap保存到具有给定文件名的文件中。 //bool QPixmap::save(QIODevice *device,
const char *format = nullptr, int quality = -1) const QPixmap scald = pix.scaled
(40,40,Qt::IgnoreAspectRatio,Qt::FastTransformation); //参数3指定缩放类型,是否考虑纵横比
//参数4指定变换类型,变换是瞬变还是渐变 //QPixmap QPixmap::scaled(const QSize &size,
Qt::AspectRatioMode aspectRatioMode = Qt::IgnoreAspectRatio,
Qt::TransformationMode transformMode = Qt::FastTransformation) const QPixmap
scaldh= pix.scaledToHeight(59);//返回图像的缩放副本。使用指定的变换模式将返回的图像缩放到给定的高度。
//pixmap的宽度将自动计算,以便保留pixmap的纵横比。 //QPixmap QPixmap::scaledToWidth(int width,
Qt::TransformationMode mode = Qt::FastTransformation) const QRegion region =
QRegion(QRect(20,50,20,20)); pix.scroll( 50,20,QRect(20,20,40,40),®ion);
//按(参数1,参数2)滚动此pixmap的区域矩形。原区域保持不变。 //参数3用来保存滚动后的区域 当pixmap上有活动画师时,无法滚动。 pix.
setMask(mask2);//设置遮罩位图。此函数将遮罩与pixmap的alpha通道合并。
//遮罩上的像素值为1表示pixmap的像素不变;值为0表示像素是透明的。遮罩的大小必须与此pixmap相同。
//设置空遮罩会重置遮罩,使以前透明的像素保持黑色。 qDebug()<<pix.size(); //返回图片尺寸 qDebug()<<pix.width();
//返回图片宽度 pix.swap(scald); //用这个pixmap交换另一个pixmap。 pix.swap(scald); QImage img2 =
pix.toImage();//将pixmap转换为QImage。如果转换失败,则返回空图像。 QPixmap trans = pix.transformed
(QTransform()); //返回使用给定转换和转换模式转换的pixmap的副本 //原图不变,返回变换后的图片 //QPixmap
QPixmap::transformed(const QMatrix &matrix, Qt::TransformationMode mode =
Qt::FastTransformation) const free(temp); }

技术
下载桌面版
GitHub
百度网盘(提取码:draw)
Gitee
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:766591547
关注微信