c# - Winforms: Smooth the rounded edges for panel -


i have followed this tutorial in order create rounded panel. code in tutorial in vb able convert c# here code:

    public class spanel : panel {     pen pen;     float penwidth = 2.0f;     int _edge = 20;     color _bordercolor = color.white;     public int edge     {                 {             return _edge;         }         set         {             _edge = value;             invalidate();         }     }      public color bordercolor     {                 {             return _bordercolor;         }         set         {             _bordercolor = value;             pen = new pen(_bordercolor, penwidth);             invalidate();         }     }      public spanel()     {         pen = new pen(_bordercolor, penwidth);     }      protected override void onpaint(painteventargs e)     {         base.onpaint(e);         extendeddraw(e);         //drawborder(e.graphics);     }      private void extendeddraw(painteventargs e)     {         e.graphics.smoothingmode = smoothingmode.antialias;         graphicspath path = new graphicspath();          path.startfigure();         path.startfigure();         path.addarc(getleftupper(edge), 180, 90);         path.addline(edge, 0, width - edge, 0);         path.addarc(getrightupper(edge), 270, 90);         path.addline(width, edge, width, height - edge);         path.addarc(getrightlower(edge), 0, 90);         path.addline(width - edge, height, edge, height);         path.addarc(getleftlower(edge), 90, 90);         path.addline(0, height - edge, 0, edge);         path.closefigure();          region = new region(path);     }      rectangle getleftupper(int e)     {         return new rectangle(0, 0, e, e);     }     rectangle getrightupper(int e)     {         return new rectangle(width - e, 0, e, e);     }     rectangle getrightlower(int e)     {         return new rectangle(width - e, height - e, e, e);     }     rectangle getleftlower(int e)     {         return new rectangle(0, height - e, e, e);     }      void drawsingleborder(graphics graphics)     {         graphics.drawarc(pen, new rectangle(0, 0, edge, edge), 180, 90);         graphics.drawarc(pen, new rectangle(width - edge -1, -1, edge, edge), 270, 90);         graphics.drawarc(pen, new rectangle(width - edge - 1, height - edge - 1, edge, edge), 0, 90);         graphics.drawarc(pen, new rectangle(0, height - edge - 1, edge, edge), 90, 90);          graphics.drawrectangle(pen, 0.0f, 0.0f, width - 1, height - 1);     }      void drawborder(graphics graphics)     {         drawsingleborder(graphics);     }  } 

i did not use border result same. here ss:

enter image description here

i thought smoothing anti alias trick guess wrong. question how can smooth edges?

i able solve following this link. downloaded sample project , created new panel. copied had on form's onpaint new panel's onpaint , have smooth edges.

public class spanel : panel {     protected override void onpaint(painteventargs e)     {         graphics g = e.graphics;         g.smoothingmode = smoothingmode.antialias;         g.fillroundedrectangle(new solidbrush(color.white), 10, 10, this.width - 40, this.height - 60, 10);         solidbrush brush = new solidbrush(             color.white             );         g.fillroundedrectangle(brush, 12, 12, this.width - 44, this.height - 64, 10);         g.drawroundedrectangle(new pen(controlpaint.light(color.white, 0.00f)), 12, 12, this.width - 44, this.height - 64, 10);         g.fillroundedrectangle(new solidbrush(color.white), 12, 12 + ((this.height - 64) / 2), this.width - 44, (this.height - 64)/2, 10);     } } 

here graphicsextension class if link ever broken.

static class graphicsextension {     private static graphicspath generateroundedrectangle(         graphics graphics,          rectanglef rectangle,          float radius)     {         float diameter;         graphicspath path = new graphicspath();         if (radius <= 0.0f)         {             path.addrectangle(rectangle);             path.closefigure();             return path;         }         else         {             if (radius >= (math.min(rectangle.width, rectangle.height)) / 2.0)                 return graphics.generatecapsule(rectangle);             diameter = radius * 2.0f;             sizef sizef = new sizef(diameter, diameter);             rectanglef arc = new rectanglef(rectangle.location, sizef);             path.addarc(arc, 180, 90);             arc.x = rectangle.right - diameter;             path.addarc(arc, 270, 90);             arc.y = rectangle.bottom - diameter;             path.addarc(arc, 0, 90);             arc.x = rectangle.left;             path.addarc(arc, 90, 90);             path.closefigure();         }         return path;     }     private static graphicspath generatecapsule(         graphics graphics,          rectanglef baserect)     {         float diameter;         rectanglef arc;         graphicspath path = new graphicspath();         try         {             if (baserect.width > baserect.height)             {                 diameter = baserect.height;                 sizef sizef = new sizef(diameter, diameter);                 arc = new rectanglef(baserect.location, sizef);                 path.addarc(arc, 90, 180);                 arc.x = baserect.right - diameter;                 path.addarc(arc, 270, 180);             }             else if (baserect.width < baserect.height)             {                 diameter = baserect.width;                 sizef sizef = new sizef(diameter, diameter);                 arc = new rectanglef(baserect.location, sizef);                 path.addarc(arc, 180, 180);                 arc.y = baserect.bottom - diameter;                 path.addarc(arc, 0, 180);             }             else path.addellipse(baserect);         }         catch { path.addellipse(baserect); }         { path.closefigure(); }         return path;     }      /// <summary>     /// draws rounded rectangle specified pair of coordinates, width, height , radius      /// arcs make rounded edges.     /// </summary>     /// <param name="brush">system.drawing.pen determines color, width , style of rectangle.</param>     /// <param name="x">the x-coordinate of upper-left corner of rectangle draw.</param>     /// <param name="y">the y-coordinate of upper-left corner of rectangle draw.</param>     /// <param name="width">width of rectangle draw.</param>     /// <param name="height">height of rectangle draw.</param>     /// <param name="radius">the radius of arc used rounded edges.</param>      public static void drawroundedrectangle(         graphics graphics,          pen pen,          float x,          float y,          float width,          float height,          float radius)     {         rectanglef rectangle = new rectanglef(x, y, width, height);         graphicspath path = graphics.generateroundedrectangle(rectangle, radius);         smoothingmode old = graphics.smoothingmode;         graphics.smoothingmode = smoothingmode.antialias;         graphics.drawpath(pen, path);         graphics.smoothingmode = old;     }      /// <summary>     /// draws rounded rectangle specified pair of coordinates, width, height , radius      /// arcs make rounded edges.     /// </summary>     /// <param name="brush">system.drawing.pen determines color, width , style of rectangle.</param>     /// <param name="x">the x-coordinate of upper-left corner of rectangle draw.</param>     /// <param name="y">the y-coordinate of upper-left corner of rectangle draw.</param>     /// <param name="width">width of rectangle draw.</param>     /// <param name="height">height of rectangle draw.</param>     /// <param name="radius">the radius of arc used rounded edges.</param>      public static void drawroundedrectangle(         graphics graphics,          pen pen,          int x,          int y,          int width,          int height,          int radius)     {         graphics.drawroundedrectangle(             pen,              convert.tosingle(x),              convert.tosingle(y),              convert.tosingle(width),              convert.tosingle(height),              convert.tosingle(radius));     }      /// <summary>     /// fills interior of rounded rectangle specified pair of coordinates, width, height     /// , radius arcs make rounded edges.     /// </summary>     /// <param name="brush">system.drawing.brush determines characteristics of fill.</param>     /// <param name="x">the x-coordinate of upper-left corner of rectangle fill.</param>     /// <param name="y">the y-coordinate of upper-left corner of rectangle fill.</param>     /// <param name="width">width of rectangle fill.</param>     /// <param name="height">height of rectangle fill.</param>     /// <param name="radius">the radius of arc used rounded edges.</param>      public static void fillroundedrectangle(         graphics graphics,          brush brush,          float x,          float y,          float width,          float height,          float radius)     {         rectanglef rectangle = new rectanglef(x, y, width, height);         graphicspath path = graphics.generateroundedrectangle(rectangle, radius);         smoothingmode old = graphics.smoothingmode;         graphics.smoothingmode = smoothingmode.antialias;         graphics.fillpath(brush, path);         graphics.smoothingmode = old;     }      /// <summary>     /// fills interior of rounded rectangle specified pair of coordinates, width, height     /// , radius arcs make rounded edges.     /// </summary>     /// <param name="brush">system.drawing.brush determines characteristics of fill.</param>     /// <param name="x">the x-coordinate of upper-left corner of rectangle fill.</param>     /// <param name="y">the y-coordinate of upper-left corner of rectangle fill.</param>     /// <param name="width">width of rectangle fill.</param>     /// <param name="height">height of rectangle fill.</param>     /// <param name="radius">the radius of arc used rounded edges.</param>      public static void fillroundedrectangle(         graphics graphics,          brush brush,          int x,          int y,          int width,          int height,          int radius)     {         graphics.fillroundedrectangle(             brush,              convert.tosingle(x),              convert.tosingle(y),              convert.tosingle(width),              convert.tosingle(height),              convert.tosingle(radius));      } } 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -