首先在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/