qt缩放qimage图像到100x100,空白处填充
时间: 2023-05-11 17:05:37 浏览: 150
您可以使用QImage的scaled函数来缩放图像,然后使用fill函数来填充空白处。以下是示例代码:
QImage image("your_image_path");
QImage scaledImage = image.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
scaledImage.fill(Qt::white);
相关问题
qt缩放200x100的图像到100x100,空白处填充的代码
你可以使用Qt中的QImage::scaled函数来缩放图像并填充空白处。以下是一个示例代码:
QImage image("your_image_path");
QImage scaledImage = image.scaled(100, 100, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
QImage resultImage(100, 100, QImage::Format_ARGB32);
resultImage.fill(Qt::white);
QPainter painter(&resultImage);
painter.drawImage((resultImage.width() - scaledImage.width()) / 2, (resultImage.height() - scaledImage.height()) / 2, scaledImage);
painter.end();
这段代码将会把一个200x100的图像缩放到100x100,并在空白处填充白色。
qt缩放200x100的图像到100x100,空白处填充透明的代码
下面是实现该功能的代码:
```cpp
QImage scaledImage = originalImage.scaled(100, 100, Qt::KeepAspectRatio, Qt::SmoothTransformation);
QImage resultImage(100, 100, QImage::Format_ARGB32);
resultImage.fill(Qt::transparent);
QPainter painter(&resultImage);
painter.drawImage(QPoint((100 - scaledImage.width()) / 2, (100 - scaledImage.height()) / 2), scaledImage);
painter.end();
```
这段代码使用 Qt 库中的 QImage 和 QPainter 类来实现图像的缩放和填充透明。首先,使用 scaled() 函数将原始图像缩放到指定大小。然后,创建一个新的 QImage 对象,设置其大小为 100x100,并用 fill() 函数将其填充为透明。最后,使用 QPainter 类将缩放后的图像绘制到新的 QImage 对象中心位置。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)