2015-11-22 13 views
6

में एक गोलाकार आयताकार कैसे आकर्षित करें मैं एक गोलाकार आयताकार बनाने के लिए इस कोड का उपयोग कर रहा हूं। लेकिन यह केवल रेक्टनलेज के ऊपरी बाएं और दाएं कोनों को खींचता है, और यह कम भाग पर आयत को पूरा नहीं करता है। इसे कैसे पूरा और भरने के लिए। मुझे क्या परिवर्तन करना चाहिए?सी #

public static Bitmap DrawRoundedRectangle(Bitmap Image, Color BoxColor, int XPosition, int YPosition, 
     int Height, int Width, int CornerRadius) 
    { 
    Bitmap NewBitmap = new Bitmap(Image, Image.Width, Image.Height); 
    using (Graphics NewGraphics = Graphics.FromImage(NewBitmap)) 
    { 
     using (Pen BoxPen = new Pen(BoxColor)) 
     { 
      using (GraphicsPath Path = new GraphicsPath()) 
      { 
        Path.AddLine(XPosition + CornerRadius, YPosition, XPosition + Width - (CornerRadius * 2), YPosition); 
        Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition, CornerRadius * 2, CornerRadius * 2, 270, 90); 
        Path.AddLine(XPosition + Width, YPosition + CornerRadius, XPosition + Width, YPosition + Height - (CornerRadius * 2)); 
        Path.AddArc(XPosition + Width - (CornerRadius * 2), YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 0, 90); 
       Path.AddLine(XPosition + Width - (CornerRadius * 2), YPosition + Height, XPosition + CornerRadius, YPosition + Height); 
        Path.AddArc(XPosition, YPosition + Height - (CornerRadius * 2), CornerRadius * 2, CornerRadius * 2, 90, 90); 
        Path.AddLine(XPosition, YPosition + Height - (CornerRadius * 2), XPosition, YPosition + CornerRadius); 
        Path.AddArc(XPosition, YPosition, CornerRadius * 2, CornerRadius * 2, 180, 90); 
        Path.CloseFigure(); 
        NewGraphics.DrawPath(BoxPen, Path); 
       } 
       } 
      } 
     return NewBitmap; 
    } 

उत्तर

17
public static GraphicsPath RoundedRect(Rectangle bounds, int radius) 
    { 
     int diameter = radius * 2; 
     Size size = new Size(diameter, diameter); 
     Rectangle arc = new Rectangle(bounds.Location, size); 
     GraphicsPath path = new GraphicsPath(); 

     if (radius == 0) 
     { 
      path.AddRectangle(bounds); 
      return path; 
     } 

     // top left arc 
     path.AddArc(arc, 180, 90); 

     // top right arc 
     arc.X = bounds.Right - diameter; 
     path.AddArc(arc, 270, 90); 

     // bottom right arc 
     arc.Y = bounds.Bottom - diameter; 
     path.AddArc(arc, 0, 90); 

     // bottom left arc 
     arc.X = bounds.Left; 
     path.AddArc(arc, 90, 90); 

     path.CloseFigure(); 
     return path; 
    } 

और इसलिए आप उन्हें हमेशा की Draw... और Fill... आकार-ड्राइंग तरीके के रूप में उपयोग कर सकते हैं आप Graphics प्रकार के लिए दो विस्तार तरीकों बना सकते हैं।

public static void DrawRoundedRectangle(this Graphics graphics, Pen pen, Rectangle bounds, int cornerRadius) 
    { 
     if (graphics == null) 
      throw new ArgumentNullException("graphics"); 
     if (pen == null) 
      throw new ArgumentNullException("pen"); 

     using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) 
     { 
      graphics.DrawPath(pen, path); 
     } 
    } 

    public static void FillRoundedRectangle(this Graphics graphics, Brush brush, Rectangle bounds, int cornerRadius) 
    { 
     if (graphics == null) 
      throw new ArgumentNullException("graphics"); 
     if (brush == null) 
      throw new ArgumentNullException("brush"); 

     using (GraphicsPath path = RoundedRect(bounds, cornerRadius)) 
     { 
      graphics.FillPath(brush, path); 
     } 
    } 
+0

धन्यवाद काम ठीक है – phpnet