Attempt to create a circular mask over the image
-
I currently have an image and I wish to place a circular mask over it , so that anything anything outside the circular mask becomes transparent. This will give my image a circular effect. This is how I am attempting to do it. However This just draws a circle over the image
@QPixmap Masking::GetNew(QImage& img)
{
img.convertToFormat(QImage::Format_ARGB32);QPixmap source = QPixmap::fromImage(img);
source = source.scaled(100,100,Qt::AspectRatioMode::IgnoreAspectRatio,Qt::TransformationMode::SmoothTransformation);QPainter painter(&img);
QBrush brsh;
QColor colr;
colr.setRgb(255,255,255,255); //white
brsh.setColor(colr);painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
painter.setBrush(brsh);painter.drawEllipse(0,0,source.width()-5,source.width()-5);
return QPixmap::fromImage(img); //Thisshouldhave the effect
}
@Any suggestion on what I might be doing wrong here. The above function is suppose to take in an image and apply a circular mask over it. and return the resulting image.
-
Take a look at this code and try it - is that what you want?
@
QPixmap GetNew(QImage& img)
{
img.convertToFormat(QImage::Format_ARGB32);
//blank copy image
QImage imageOut(img.size(),QImage::Format_ARGB32);
//painter on it
QPainter painter(&imageOut);
//set opacity
painter.setOpacity(0.5);
painter.setBrush(Qt::white);
painter.setPen(Qt::NoPen);
//draw transparent image
painter.drawImage(0,0,img);
//set opacity
painter.setOpacity(1);
QPainterPath path(QPointF(100,100));
//your mask - ellipse
path.addEllipse(100,100,img.width()-200, img.height()-200);
painter.setClipPath(path);
//draw untransparent part of image
painter.drawImage(0,0,img);
return QPixmap::fromImage(imageOut);
}
@