VB.net该如何将图片圆形显示?

做了一个获取头像的软件,想把这个头像改为像手机一样的圆形,该怎么做呢?

新建一个Winform,拖两个图片框。图片框1在设计器中选择一个图片(尺寸在图片框容纳为佳)。如下代码测试通过:

        private void button1_Click(object sender, EventArgs e)
        {
            Image img1 = this.pictureBox1.Image;
            Image img2 = CropToCircle(img1);
            this.pictureBox2.Image = img2;
        }

        public Image CropToCircle(Image img1)
        {
            Image img2 = new Bitmap(img1.Width, img1.Height, 
            img1.PixelFormat);
            Graphics g = Graphics.FromImage(img2);
            using (Brush br = 
            new SolidBrush(SystemColors.Control))//背景色
            {
                g.FillRectangle(br, 0, 0, 
                img2.Width, img2.Height);
            }
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, img2.Width, img2.Height);
            g.SetClip(path);
            g.DrawImage(img1, 0, 0);
            return img2;
        }

温馨提示:答案为网友推荐,仅供参考
相似回答