Home » Windows OS

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

      //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)
        if ((corners & Corners.TopLeft) == Corners.TopLeft)
          path.AddArc(bounds.Left, bounds.Top, radius, radius, 180, 90);
          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);
          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);
          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);
          path.AddLine(bounds.Left, bounds.Bottom, bounds.Left, bounds.Bottom);


      return path;


    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 


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 


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.



I have designed dialog box without title bar from
dialog editor for MFC application. And also set the
image on dialog box. It is there rectangle shape.

Now I need to change this to rounded rectangle shape.

Just corners shoud be rounded. How to do this?

Thanks in advance.


I'm trying to calculate a customer tax.

Select 100 * Round(25296 /100 ,1) --returns 25200

 however Math.Round in Visual Basic, return 25300.

 Result = 100 * Math.Round(25296 / 100) ' returns 25300

I really nead any appropriate function to fix it.

Plese help soon.




As far as i know, most of the rectangle that we draw in viewport3D will be in the middle of the screen. How do I draw it on my top left viewport3D ??



       <Viewport3D Width="Auto" Height="Auto" Canvas.Top="1" Name="mainViewport">
                      Position="0.5,0.5,1" LookDirection="0,0,-1"
                          Direction="-2,-3,-1" />



In my code behind, I draw the rectangle (2D look) :

rectangleMesh.Positions.Add(new Point3D(0, 0, 0));
rectangleMesh.Positions.Add(new Point3D(0.3, 0, 0));
rectangleMesh.Positions.Add(new Point3D(0.3, 0.3, 0));
rectangleMesh.Positions.Add(new Point3D(0, 0.3, 0));

I would like to draw the shape in my canvas position x=0 , y=0


We use fancy focus indicators in our controls, and since our controls may change their outline dramatically during runtime, we place triggers on "IsKeyboardFocused" property to shape our focus indicators.

The problem is that when such a method is used, the focus indicator is drawn when user clicks on the control. The expected behaviour would be that the focus indicator is drawn only when focus is received via keyboard. We tried placing triggers on other properties (IsFocused, etc.) but it did not work.

In summary, how can one draw a "focus indicator" only when control receives focus via keyboard, not when 'via mouse' (without using FocusVisualStyle)? Or does any alternative approach exists?

The solution should be very simple, so probably we are missing something. Does anyone have any idea?


Thanks in advance,



Hi friends,, 

I am working on c# desktop Application in which I want image editor functionality like I have image and on it I want draw circle rectangle ,arrow and ellipse . I am able to draw it but only once at a time . if i select circle than i cant draw circle more than one and other also like rectangle ,arrow 
. for this I took refrence of cropcircle test application. 

on mouse down that is replacing previus value to new . how can i REMOVE THIS PROBLEM.... 
SUGGEST me any test application. 

Hi..I am after a paint like application in wpf.I want to draw basic shapes in an inkcanvas for eg:line,ellipse or a rectangle.Can anyone guide me and provide some clues on how to do it.I also want to insert a text into the canvas.Please help me on this.Any inputs will be greatly appreciated.

Hi I have tried to draw an rubberband rectangle on a form using the mouse in C#.


1) After the mouse release the rectangle disappears. [I want it to stay on the form]

2) I also need to find the coordinates of the four points of the drawn rectangle

3) I also need to erase the rectangle to draw a new one when necessary


Form :





using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;

namespace rubberbandrectangle
public partial class Form1 : Form
  Boolean bHaveMouse;
  Point ptOriginal = new Point();
  Point ptLast = new Point();

  public Form1()

  private void Form1_MouseDown(object sender, MouseEventArgs e)
    bHaveMouse = true;
    ptOriginal.X = e.X;
    ptOriginal.Y = e.Y;
    ptLast.X = -1;
    ptLast.Y = -1;

  private void MyDrawReversibleRectangle(Point p1, Point p2)
    Rectangle rc = new Rectangle();

    p1 = PointToScreen(p1);
    p2 = PointToScreen(p2);
    if (p1.X < p2.X)
      rc.X = p1.X;
      rc.Width = p2.X - p1.X;
      rc.X = p2.X;
      rc.Width = p1.X - p2.X;
    if (p1.Y < p2.Y)
      rc.Y = p1.Y;
      rc.Height = p2.Y - p1.Y;
      rc.Y = p2.Y;
      rc.Height = p1.Y - p2.Y;
            Color.Red, FrameStyle.Dashed);

  private void Form1_MouseUp(object sender, MouseEventArgs e)
    bHaveMouse = false;
    if (ptLast.X != -1)
      Point ptCurrent = new Point(e.X, e.Y);
      MyDrawReversibleRectangle(ptOriginal, ptLast);
    ptLast.X = -1;
    ptLast.Y = -1;
    ptOriginal.X = -1;
    ptOriginal.Y = -1;

  private void Form1_MouseMove(object sender, MouseEventArgs e)
    Point ptCurrent = new Point(e.X, e.Y);
    if (bHaveMouse)
      if (ptLast.X != -1)
        MyDrawReversibleRectangle(ptOriginal, ptLast);
      ptLast = ptCurrent;
        MyDrawReversibleRectangle(ptOriginal, ptCurrent);

  private void Form1_Load(object sender, EventArgs e)
    MouseDown += new MouseEventHandler(Form1_MouseDown);
    MouseUp += new MouseEventHandler(Form1_MouseUp);
    MouseMove += new MouseEventHandler(Form1_MouseMove);
    bHaveMouse = false;

Thanks for reading


hello to all

can any one tell me that how can i draw a rectangle and some text in compact c#.


bikram singh

i am making win 32 bit application, and want to draw a green rectangle.Pen function? my code is short. Is this done in wm paint. or do i need events  argument in paint. please send me tiny code snippet. thanks.

I used a control to draw a rectangle, which is fine.  Is there a way to draw the same rectangle in C# without a control and place it at a particular row and column on the grid?  I know I can use XAML to do this, but what about straight C# code?  All the examples online don't seem to show a starting position for placement.  'Line' is not a control and so it can be drawn at a particular row and column.  What about a rectangle?  Rectangle doesn't seem to have any row and col fields like 'Line' does.




I'm using a form with a 0.01 opacity and I want to draw a rectangle on it... but, the rectangle's border, just like the form, also has a low opacity, which in my case makes it invisible.
Is there a way to draw the rectangle without inherit the form's properties?

thank you

I have an Image control.

How to draw a rectangle over the image. The rectangle should not be of a fixed size.




I have a datagridview in my application. and I have created a function for searching different words in datagridview. When the functionfind the word in one cell of datagridview, the cell is selected, but I want  to draw a red rectangle in the selected cell when I found the searched value. Can somebody help me?

I have made my application in visual basic. It will be very helpfull if I will get a solution in VB

Thanks a lot


Well...I know how to draw a rectangle with four random integer or floating numbers and then you could create a graphic variable and use it with bitmap and create a permanent image. However. If you are to be given two points and these two points are the mouse movement with a click - That's, you click anywhere with your mouse to create a first point and then move your mouse again and click it to create a second point. Thus the rectangle is drawn that connects the first point with the second point.

For instance, if your first point is topleft and your second point is downright, you create a rectangle with these two points.





How can you do that? I just get stuck with this because I just wanna learn and draw something special on the picturebox with bitmap as my image.


I am writing a piece of text to a rectangle with DrawText

      heightOfText = pDC->DrawText(strSubject, strSubject.GetLength(),<br/>
                     DT_LEFT | DT_WORDBREAK | DT_NOPREFIX);

but I now want to be able to draw some bitmaps after the end of the drawn text.

I can get the cx, cy of the text using

subSize = pDC->GetTextExtent(strSubject, strSubject.GetLength());

but that has no relationship to the rectangle and does not take account of the text when drawn with DT_WORDBREAK.

heightOfText tells me how many lines were used to draw the text, but it does not tell me the width of the last line drawn.

How can I find this out?




Dear All,

I would Like to know how to draw lines, circles and other objects in windows forms

I have got some sample and i tried it in my code form load but its not working Its compailing all right but not showing any line


Please have a look at my code

privatevoid Form1_Load(object sender, EventArgs e)


   System.Drawing.Pen myPen;

   myPen = new System.Drawing.Pen(System.Drawing.Color.Red);

   System.Drawing.Graphics formGraphics = this.CreateGraphics();

   formGraphics.DrawLine(myPen,200, 200, 200, 200);

   formGraphics.DrawString("Welcome C#", new Font("Verdana", 20),

   new SolidBrush(Color.Tomato), 40, 40);




can any one help me with this

hi everyone

i want to make a form and i want to draw a rectangle at run time (the user click a button to draw the rectangle on the form) and add an event (click,r click,etc..) to this rectangle

any idea please

thanx in advance



i have to draw too many rectangles which are about 20000. i set their properties from a table of my Database frequently be cause the properties may be changing by users at real time.


as first Solution i utilize a loop which repeat as same as count of my Table. of course it was Easy way , but it is so slow solution and not logical (i think). please tell me what's your solution? is there faster solution?


Hello .

I have a palete array and i want to display all palette colors in to an another dlg box. for that i am traying to draw rectangle on dlg box.

But i did nt get it . So how can draw  rectangle on Dlg box.

Please give me some hint for that.

Thanking you in advance.

Thanking you in advace.



I have a problem. I am creating  a web page (using asp.NET C#). I have to draw shapes (mainly rectangles). I use drawing dll from c# to draw the rectangles (Using graphics.drawRect()). Currently I am able to display the rectangles. I use graphics.drawstring to add text on these rectangles. However, I would like to add  hyperlinks on the text of every rectangle. How can I do that? Thank you.


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure