Home » Windows OSRSS

Draw a sharp rounded rectangle in .Net.

Hello I am working in .Net 2010 framework 2.0.

I want the graphicspath for rounded rectangle corners. There are lot of example present on internet but  problem is that they are not sharp rounded corner rectangle. If a sets the radius for rounded corner (in Float or points)  between2 and 3, we can see that all the rectangle corners are not same(After zooming). One or two corners are always having different rounded corners as compared with other two. If I am using the Win32 functions to draw the same, it works fine but I am not able to get the graphics path from win32 functions.


Can anyone suggest me how to draw the Sharp Rounded Rectangle?

 

Thanks in advance

 

6 Answers Found

 

Answer 1

by win32 functions are you referring to the GDI+ functions? Like CreateRoundRectRgn(...)?

as far as I know, there is no easy way to go from a region back to graphics path. I do know the region class contains a couple methods:

GetRegionData and GetRegionScans that might help.

 

 

Answer 2

Thanks for the reply,

The same problem I am finding with win32. Also I found that there is no Antialise in GDI. So I want to use .Net function with GDI+ by creating the lines and arc.

So I am trying to use combination of lines and arcs to make the rounded  corners.

Can you suggest anything?

 

Thanks for the reply again.

 

Answer 3

I use the following method which appears to create equal corners when I ScaleTransform the graphics object.

 

publicstatic GraphicsPath RoundedRectangle(Rectangle bounds, float radius, Corners corners)
    {
      //Make sure the Path fits inside the rectangle
      bounds.Width--;
      bounds.Height--;

      //Scale the radius if it's too large to fit.if (radius > (bounds.Width))
        radius = bounds.Width;
      if (radius > (bounds.Height))
        radius = bounds.Height;

      System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();

      if (radius <= 0)
        path.AddRectangle(bounds);
      else
      {
        if ((corners & Corners.TopLeft) == Corners.TopLeft)
          path.AddArc(bounds.Left, bounds.Top, radius, radius, 180, 90);
        else
          path.AddLine(bounds.Left, bounds.Top, bounds.Left, bounds.Top);

        if ((corners & Corners.TopRight) == Corners.TopRight)
          path.AddArc(bounds.Right - radius, bounds.Top, radius, radius, 270, 90);
        else
          path.AddLine(bounds.Right, bounds.Top, bounds.Right, bounds.Top);

        if ((corners & Corners.BottomRight) == Corners.BottomRight)
          path.AddArc(bounds.Right - radius, bounds.Bottom - radius, radius, radius, 0, 90);
        else
          path.AddLine(bounds.Right, bounds.Bottom, bounds.Right, bounds.Bottom);

        if ((corners & Corners.BottomLeft) == Corners.BottomLeft)
          path.AddArc(bounds.Left, bounds.Bottom - radius, radius, radius, 90, 90);
        else
          path.AddLine(bounds.Left, bounds.Bottom, bounds.Left, bounds.Bottom);
      }

      path.CloseFigure();

      return path;

    }

    [Flags]
    publicenum Corners
    {
      None = 0,
      TopLeft = 1,
      TopRight = 2,
      BottomLeft = 4,
      BottomRight = 8,
      All = TopLeft | TopRight | BottomLeft | BottomRight
    }

 

 

Answer 4

I messed with the drawLine, drawArc functions a while back when creating a usercontrol, with no success.. I could get the control to look ok at one size but there was always another size that would distort the rounded  edges. I use these functions from GDI+ for rounding..

    [System.Runtime.InteropServices.DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    privatestaticextern System.IntPtr CreateRoundRectRgn
      ( int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse );

    [System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
    privatestaticexternbool DeleteObject(System.IntPtr hObject);

System.IntPtr ptrButton = CreateRoundRectRgn(_LeftRect, _TopRect, _RightRect, _BottomRect, _WidthRadius, _HeightRadius);

The IntPtr can then be assigned or unioned (etc) to the controls Region.


Like I mentioned before, I know of no way to directly convert GraphicsPath to Region.

Good Luck!

 

 

Answer 5

Thanks for the reply.

 

I found this example 

http://www.geekpedia.com/code112_Draw-Rounded-Corner-Rectangles-Using-Csharp.html

This is not the perfect but i found this as better than the other attempts.

 

Thanks for the reply again.

 

Answer 6

Thanks for the reply.

 

I found this example 

http://www.geekpedia.com/code112_Draw-Rounded-Corner-Rectangles-Using-Csharp.html

This is not the perfect but i found this as better than the other attempts.

 

Thanks for the reply again.

Better than my example posted above?

I'll admit that I didn't give you the code to actually draw  the rectangle, but I assumed you knew how to set Graphics.SmoothingMode and call Graphics.Drawpath().

The method you link to looks exactly the same as every generic DrawRoundedRectangle routine that I have seen.

It does not compensate for you putting in a larger radius than the rectangle  supports.

There is no need to add the lines as the arcs will automatically be joined by the CloseFigure() command.

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter