C#实现将图片设置成圆形形式显示
程序员文章站
2022-08-04 13:26:01
首先在Form中添加一个控件,然后将控件的背景BackColor设置成透明 、 然后分别设置控件的Image: Image image = Image.FromFile(UserLoginInfor.LoginInfor.userIco); 获取image的width、height:width= i ......
首先在form中添加一个控件,然后将控件的背景backcolor设置成透明
、
然后分别设置控件的image: image image = image.fromfile(userlogininfor.logininfor.userico);
获取image的width、height:width= image.width、height=image.height
然后实现该功能,具体代码如下:(picturebox1:控件的名称)
image image = image.fromfile(userlogininfor.logininfor.userico); this.picturebox1.image = cutellipse(image.fromfile(userlogininfor.logininfor.userico), new rectangle(0, 0,image .width, image .height), new size(image .width, image .height));
最后创建一个自定义方法cutellipse:
private image cutellipse(image img, rectangle rec, size size)
{
bitmap bitmap = new bitmap(size.width, size.height);
using (graphics g = graphics.fromimage(bitmap))
{
using (texturebrush br = new texturebrush(img, system.drawing.drawing2d.wrapmode.clamp, rec))
{
br.scaletransform(bitmap.width / (float)rec.width, bitmap.height / (float)rec.height);
g.smoothingmode = system.drawing.drawing2d.smoothingmode.highquality;
g.fillellipse(br, new rectangle(point.empty, size));
}
}
return bitmap;
}
参考网址:https://q.cnblogs.com/q/11565/