如下:参数2是一个多边形区域
QPixmap CommonFunc::cropPixmapByPolygon(const QPixmap& source, const QPolygon& polygon)
{
// 检查源图片是否有效
if (source.isNull()) {
return QPixmap();
}
// 检查多边形是否有效
if (polygon.isEmpty()) {
return QPixmap();
}
// 获取多边形的边界矩形,用于确定新图片的大小
QRect polygonRect = polygon.boundingRect();
// 创建新的QPixmap,大小为多边形的边界矩形
QPixmap cropped(polygonRect.size());
cropped.fill(Qt::transparent); // 初始化为透明
// 创建QPainter用于绘制
QPainter painter(&cropped);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
// 创建裁剪路径
QPainterPath path;
path.addPolygon(polygon.translated(-polygonRect.topLeft())); // 平移多边形到新图片的坐标系统
// 设置裁剪区域
painter.setClipPath(path);
// 将源图片中多边形区域绘制到新图片上
painter.drawPixmap(QPoint(0,0), source, polygonRect);
return cropped;
}
5698

被折叠的 条评论
为什么被折叠?



