C# 图片切割成圆形图片的两种方式

private void Form1_Load(object sender, EventArgs e)
        {
            string file =System.IO.Path.Combine(Environment.CurrentDirectory, @"11.jpg");
            try
            {
                Image i = new Bitmap(file);
                pbO.Image = i;

                pbD1.Image = WayOne(file);
                pbD2.Image = WayTwo(file);

                pbS1.Image = WaySOne(file);
                pbS2.Image = WaySTwo(file);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


private Bitmap WayOne(string file)
        {
            using (Image i = new Bitmap(file))
            {
                Bitmap b = new Bitmap(i.Width, i.Height);
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.FillEllipse(new TextureBrush(i), 0, 0, i.Width, i.Height);
                }
                return b;
            }
        }


 private Bitmap WayTwo(string file)
        {
            using (Image i = new Bitmap(file))
            {
                Bitmap b = new Bitmap(i.Width, i.Height);
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.DrawImage(i, 0, 0, b.Width, b.Height);
                    int r = Math.Min(b.Width, b.Height) / 2;
                    PointF c = new PointF(b.Width / 2.0F, b.Height / 2.0F);
                    for (int h = 0; h < b.Height; h++)
                        for (int w = 0; w < b.Width; w++)
                            if ((int)Math.Pow(r, 2) < ((int)Math.Pow(w * 1.0 - c.X, 2) + (int)Math.Pow(h * 1.0 - c.Y, 2)))
                            {
                                b.SetPixel(w, h, Color.Transparent);
                            }
                }
                return b;
            }
        }

 private Bitmap WaySOne(string file)
        {
            using (Image i = new Bitmap(file))
            {
                Bitmap b = new Bitmap(i.Width, i.Height);
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    using (System.Drawing.Drawing2D.GraphicsPath p = new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Alternate))
                    {
                        p.AddEllipse(0, 0, i.Width, i.Height);
                        g.FillPath(new TextureBrush(i), p);
                    }
                    //g.FillEllipse(new TextureBrush(i), 0, 0, i.Width, i.Height);
                }
                return b;
            }
        }


private Bitmap WaySTwo(string file)
        {
            using (Image i = new Bitmap(file))
            {
                Bitmap b = new Bitmap(i.Width, i.Height);
                using (Graphics g = Graphics.FromImage(b))
                {
                    g.DrawImage(i, 0, 0, b.Width, b.Height);
                    int r = Math.Min(b.Width, b.Height) / 2;
                    PointF c = new PointF(b.Width / 2.0F, b.Height / 2.0F);
                    for (int h = 0; h < b.Height; h++)
                        for (int w = 0; w < b.Width; w++)
                            if ((int)Math.Pow(r, 2) < ((int)Math.Pow(w * 1.0 - c.X, 2) + (int)Math.Pow(h * 1.0 - c.Y, 2)))
                            {
                                b.SetPixel(w, h, Color.Transparent);
                            }
                    //画背景色圆
                    using (Pen p = new Pen(System.Drawing.SystemColors.Control))
                        g.DrawEllipse(p, 0, 0, b.Width, b.Height);
                }
                return b;
            }
        }

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,可以使用Magick.NET库来实现将图片剪切为圆形的功能。Magick.NET是一个.NET平台下的开源图像处理库,提供了各种各样的图像处理功能,包括将图片剪切为圆形的功能。 以下是一个简单的示例代码,用来将一张图片剪切为圆形: ```csharp using ImageMagick; MagickImage image = new MagickImage("input.jpg"); // 加载图片 // 将图片剪切为圆形 image.Alpha(AlphaOption.Set); // 设置图片透明度 image.VirtualPixelMethod = VirtualPixelMethod.Transparent; // 设置虚拟像素方法 image.BackgroundColor = MagickColors.None; // 设置背景颜色为透明 image.Resize(200, 200); // 调整图片大小为200x200 image.Crop(new MagickGeometry(0, 0, 200, 200), Gravity.Center); // 剪切图片为正方形 image.Extent(200, 200, MagickColors.None); // 设置图片大小为200x200 image.Draw(new DrawableCircle(100, 100, 100, 1)); // 画一个半径为100的圆形 image.Write("output.png"); // 保存图片 ``` 在这个示例中,我们首先加载了一张名为"input.jpg"的图片。然后,我们使用了Magick.NET库提供的各种方法来将这张图片剪切为圆形。其中,Alpha方法用于设置图片透明度,VirtualPixelMethod属性用于设置虚拟像素方法,BackgroundColor属性用于设置背景颜色为透明,Resize方法用于调整图片大小,Crop方法用于剪切图片为正方形,Extent方法用于设置图片大小,Draw方法用于画圆形。最后,我们将处理后的图片保存为"output.png"。 你可以根据自己的需求修改代码中的图片路径、大小、圆形半径等参数,以实现不同的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值