C# 将正方形头像转化成圆形头像(例如:qq头像)

转载于:http://blog.csdn.net/chinacsharper/article/details/50854852

看图:

C# 将正方形头像转化成圆形头像(例如:qq头像)_第1张图片

点击转化后效果:

C# 将正方形头像转化成圆形头像(例如:qq头像)_第2张图片


实现代码如下:

        private void button1_Click(object sender, EventArgs e)
        {
            Image image = this.pictureBox1.Image;
            Image newImage = CutEllipse(image, new Rectangle(0, 0, 194, 194), new Size(194, 194));
            this.pictureBox2.Image = newImage;
            this.pictureBox1.Image = newImage;
        }
        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.AntiAlias;
                    g.FillEllipse(br, new Rectangle(Point.Empty, size));
                }
            }
            return bitmap;
        }


你可能感兴趣的:(c#)