如何绘制带有直角边的图形时,把直角换为平滑的曲线。  代码要精简, 

解决方案 »

  1.   

    没有现成的方法,只能拼接了:protected void Page_Load(object sender, EventArgs e)
    {
            Bitmap bm = new Bitmap(800, 600);
            Graphics g = Graphics.FromImage(bm);
            g.FillRectangle(Brushes.White,new Rectangle(0,0,800,600));
            FillRoundRectangle(g,Brushes.Plum,new Rectangle(100, 100, 100, 100), 8);
            DrawRoundRectangle(g, Pens.Yellow,new Rectangle(100, 100, 100, 100), 8);
            bm.Save(Response.OutputStream, ImageFormat.Jpeg);
            g.Dispose();
            bm.Dispose();
    }
     public static void DrawRoundRectangle(Graphics g,Pen pen,Rectangle rect, int cornerRadius)
    {
            using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
            {
                g.DrawPath(pen, path);
            }
    }
    public static void FillRoundRectangle(Graphics g, Brush brush,Rectangle rect, int cornerRadius)
    {
            using (GraphicsPath path = CreateRoundedRectanglePath(rect, cornerRadius))
            {
                g.FillPath(brush, path);
            }
    }
    internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
    {
            GraphicsPath roundedRect = new GraphicsPath();
            roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
            roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
            roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
            roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
            roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
            roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
            roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
            roundedRect.CloseFigure();
            return roundedRect;
      

  2.   

    使用API也可以:[DllImport("gdi32.Dll")]   
      public   static   extern   int   CreateRoundRectRgn(int   nLeftRect,   int   nTopRect,   int   nRightRect,   int   nBottomRect,   int   nWidthEllipse,   int   nHeightEllipse);
      

  3.   

    调用Gdi的API(RoundRect)可以直接画出:
        public partial class Form1 : Form
        {
            [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
            public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
            public Form1()
            {
                InitializeComponent();
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                RoundRect(e.Graphics.GetHdc(), 20, 20, 100, 100, 20, 20);
            }
        }
      

  4.   

     API的可以直接绘制出来么?
      

  5.   

    如下代码入到你的窗体里就可以直接运行出来了:
    [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    public static extern bool RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        RoundRect(e.Graphics.GetHdc(), 20, 20, 100, 100, 20, 20);
    }
      

  6.   

    代码来自http://bingning.net/VB/SOURCE/graphics/linejoin.htmlprotected override void OnPaint(PaintEventArgs e)
     {
         base.OnPaint(e);
     
         //Pen的作成
         Pen blackPen = new Pen(Color.Black, 10);
     
         //设定LineJoin属性为Bevel描绘四角形
         blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
         e.Graphics.DrawRectangle(blackPen, new Rectangle(70, 10, 40, 40));
     
         //设定LineJoin属性为Miter描绘四角形
         blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Miter;
         e.Graphics.DrawRectangle(blackPen, new Rectangle(130, 10, 40, 40));
     
         //设定LineJoin属性为MiterClipped描绘四角形
         blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.MiterClipped;
         e.Graphics.DrawRectangle(blackPen, new Rectangle(190, 10, 40, 40));
     
         //设定LineJoin属性为Round描绘四角形(角为圆弧)
         blackPen.LineJoin = System.Drawing.Drawing2D.LineJoin.Round;
         e.Graphics.DrawRectangle(blackPen, new Rectangle(250, 10, 40, 40));
     }
    结果如下图所示。
      

  7.   

    请问还可以调整。。四个角上的弧度吗? API上画的那弧度貌似不能调整
      

  8.   

    API 的 RoundRect  最后两个参数就是 弧度如果真不行了 你可以考虑自己绘制 可以使用 Graphics.DrawBezier()方法. 但要绘制4次才可以