网站后台显示不全,荣耀手机商城官方网站,外贸公司网站素材,专业定制网站开发公司最近使用QImage的函数setAlphaChannel时遇到了一个坑#xff0c;花了不少时间才弄清楚#xff1a;在使用这个函数后#xff0c;图像格式都会变成QImage::Format_ARGB32_Premultiplied。
先看下setAlphaChannel在帮助文档的说明#xff1a;
void QImage::setAlphaChannel(… 最近使用QImage的函数setAlphaChannel时遇到了一个坑花了不少时间才弄清楚在使用这个函数后图像格式都会变成QImage::Format_ARGB32_Premultiplied。
先看下setAlphaChannel在帮助文档的说明
void QImage::setAlphaChannel(const QImage alphaChannel)
Sets the alpha channel of this image to the given alphaChannel.
If alphaChannel is an 8 bit alpha image, the alpha values are
used directly. Otherwise, alphaChannel is converted to
8 bit grayscale and the intensity of the pixel values is used.
If the image already has an alpha channel,
the existing alpha channel is multiplied with the new one.
If the image doesnt have an alpha channel
it will be converted to a format that does.
The operation is similar to painting alphaChannel as
an alpha image over this image using QPainter::CompositionMode_DestinationIn.
大概意思
setAlphaChannel函数为图像指定透明通道如果alphaChannel是单通道的8位图片那么直接使用如果不是就转换成8位的灰度图片在作为透明通道。
如果图像已经有透明通道那么两个通道会相乘如果图像没有透明通道则会将图像转换成有透明通道的格式。
帮助文档只说了如果图像没有透明通道那么会将图像转化成有透明通道的图像但在使用过程中会发现只要使用了setAlphaChannel图像都会将格式转化成
QImage::Format_ARGB32_Premultiplied格式。
测试如下
void MainWindow::on_pushButton_clicked()
{QImage src1(100,100,QImage::Format_RGB32);QImage src2(100,100,QImage::Format_RGB16);QImage src3(100,100,QImage::Format_ARGB32);QImage alpha(100,100,QImage::Format_Grayscale8);alpha.fill(Qt::white);src1.setAlphaChannel(alpha);src2.setAlphaChannel(alpha);src3.setAlphaChannel(alpha);qDebug()(src1.format() QImage::Format_ARGB32_Premultiplied);qDebug()(src2.format() QImage::Format_ARGB32_Premultiplied);qDebug()(src3.format() QImage::Format_ARGB32_Premultiplied);
}
打印出的结果都是true也就是图像格式都转换成了QImage::Format_ARGB32_Premultiplied。