【问题标题】:How to create image with rounded corners in C#?如何在 C# 中创建带圆角的图像?
【发布时间】:2010-12-18 01:01:34
【问题描述】:

我想使用 GDI+ 创建带有圆角的图像(来自另一个图像)。最好的方法是什么?

PS:它不是用于网络的,所以我不能使用客户端 CSS

【问题讨论】:

    标签: c# .net image rounded-corners


    【解决方案1】:

    最直接的方法是使用带圆角的可缩放蒙版。将蒙版应用于图像并导出新图像。

    Here 是 CodeProject 的一篇文章,专门处理这个问题。

    【讨论】:

      【解决方案2】:

      这个功能似乎做你想做的事。如果需要,它也可以很容易地修改为返回位图。您还需要清理您不再需要的任何图像等。改编自: http://www.jigar.net/howdoi/viewhtmlcontent98.aspx

      using System.Drawing;
      using System.Drawing.Drawing2D;
      
      public Image RoundCorners(Image StartImage, int CornerRadius, Color BackgroundColor)
      {
          CornerRadius *= 2;
          Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);
          using(Graphics g = Graphics.FromImage(RoundedImage))
          {
            g.Clear(BackgroundColor);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Brush brush = new TextureBrush(StartImage);
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(0, 0, CornerRadius, CornerRadius, 180, 90);
            gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0, CornerRadius, CornerRadius, 270, 90);
            gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
            gp.AddArc(0, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
            g.FillPath(brush, gp);
            return RoundedImage;
          }
      }
      
      Image StartImage = Image.FromFile("YourImageFile.jpg");
      Image RoundedImage = this.RoundCorners(StartImage, 25, Color.White);
      //Use RoundedImage...
      

      【讨论】:

      【解决方案3】:

      使用 Graphics.SetClip() 方法是最好的方法。例如:

          public static Image OvalImage(Image img) {
              Bitmap bmp = new Bitmap(img.Width, img.Height);
              using (GraphicsPath gp = new GraphicsPath()) {
                  gp.AddEllipse(0, 0, img.Width, img.Height);
                  using (Graphics gr = Graphics.FromImage(bmp)) {
                      gr.SetClip(gp);
                      gr.DrawImage(img, Point.Empty);
                  }
              }
              return bmp;
          }
      

      【讨论】:

      • 这实际上创建了一个椭圆形或圆形的图像。很高兴知道如何做到这一点,但不是人们通常所说的“圆角”。使用此方法无需调整圆角半径。
      • 来吧,为皮特发挥你的想象力。您可以使用 GraphicsPath 通过组合弧、线、多边形和贝塞尔曲线来制作任何您想要的形状。
      【解决方案4】:

      所有其他答案都存在沿左边框和上边框 1 像素失真的问题。此代码通过在为蒙版添加弧时在左侧和顶部偏移 -1 个像素来解决此问题。

      public static Image RoundCorners(Image StartImage, int CornerRadius, Color BackgroundColor)
      {
          CornerRadius *= 2;
          Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);
      
          using(Graphics g = Graphics.FromImage(RoundedImage))
          {
             g.Clear(BackgroundColor);
             g.SmoothingMode = SmoothingMode.HighQuality;
             g.CompositingQuality = CompositingQuality.HighQuality;
             g.InterpolationMode = InterpolationMode.HighQualityBicubic;
      
             using(Brush brush = new TextureBrush(StartImage))
             {
                using(GraphicsPath gp = new GraphicsPath())
                {
                   gp.AddArc(-1, -1, CornerRadius, CornerRadius, 180, 90);
                   gp.AddArc(0 + RoundedImage.Width - CornerRadius, -1, CornerRadius, CornerRadius, 270, 90);
                   gp.AddArc(0 + RoundedImage.Width - CornerRadius, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
                   gp.AddArc(-1, 0 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
      
                   g.FillPath(brush, gp);
                }
             }
      
             return RoundedImage;
          }
      }
      

      【讨论】:

        【解决方案5】:

        我最终结合了https://stackoverflow.com/a/1759073https://stackoverflow.com/a/1759225 来获得圆形图像,因为我希望背景是透明的。想我会分享它:

        private Image RoundCorners(Image image, int cornerRadius)
        {
            cornerRadius *= 2;
            Bitmap roundedImage = new Bitmap(image.Width, image.Height);
            GraphicsPath gp = new GraphicsPath();
            gp.AddArc(0, 0, cornerRadius, cornerRadius, 180, 90);
            gp.AddArc(0 + roundedImage.Width - cornerRadius, 0, cornerRadius, cornerRadius, 270, 90);
            gp.AddArc(0 + roundedImage.Width - cornerRadius, 0 + roundedImage.Height - cornerRadius, cornerRadius, cornerRadius, 0, 90);
            gp.AddArc(0, 0 + roundedImage.Height - cornerRadius, cornerRadius, cornerRadius, 90, 90);
            using (Graphics g = Graphics.FromImage(roundedImage))
            {
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.SetClip(gp);
                g.DrawImage(image, Point.Empty);
            }
            return roundedImage;
        }
        

        【讨论】:

          【解决方案6】:

          盘旋时边缘更平滑。

          public static Image RoundCorners(Image StartImage, float CornerRadius, Color BackgroundColor)
          {
              CornerRadius *= 2;
              Bitmap RoundedImage = new Bitmap(StartImage.Width, StartImage.Height);
          
              using(Graphics g = Graphics.FromImage(RoundedImage))
              {
                 g.Clear(BackgroundColor);
                 g.SmoothingMode = SmoothingMode.HighQuality;
                 g.CompositingQuality = CompositingQuality.HighQuality;
                 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
          
                 using(Brush brush = new TextureBrush(StartImage))
                 {
                    using(GraphicsPath gp = new GraphicsPath())
                    {
                      gp.AddArc(0, -1, CornerRadius, CornerRadius, 180, 90);
                      gp.AddArc(-1 + RoundedImage.Width - CornerRadius, -1, CornerRadius, CornerRadius, 270, 90);
                      gp.AddArc(-1 + RoundedImage.Width - CornerRadius, -1 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90);
                      gp.AddArc(0, -1 + RoundedImage.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90);
          
                       g.FillPath(brush, gp);
                    }
                 }
          
                 return RoundedImage;
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-26
            • 2023-04-08
            • 1970-01-01
            • 2010-12-13
            • 1970-01-01
            • 1970-01-01
            • 2014-09-13
            • 2011-04-08
            相关资源
            最近更新 更多