欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#画圆角矩形的方法

程序员文章站 2022-03-22 14:34:10
本文实例讲述了c#画圆角矩形的方法。分享给大家供大家参考。具体实现方法如下: protected void page_load(object sender, ev...

本文实例讲述了c#画圆角矩形的方法。分享给大家供大家参考。具体实现方法如下:

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;
}

希望本文所述对大家的c#程序设计有所帮助。