C# and GDI : draw Round rectangles, fonts, ellipse, fonts

By Perry

This example draws Round rectangles, fonts and ellipse in C# using GDI. You will need to use below graphics namespaces in windows application: using System.Drawing; using System.Drawing.Drawing2D;

Complete Program: After creating windows application copy paste below code in your main code

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

namespace WindowsApplication1
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();             
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics objG = e.Graphics;
Pen objP = new Pen(Color.Blue);
Font font = new Font("Times New Roman", 26);
//draw eclipse
            Rectangle rect = new Rectangle(50, 50, 200, 100);
objG.DrawEllipse(objP, rect);             
objG.DrawString("EggHeadCafe", font, new SolidBrush(Color.Red), 14, 10);
//draw round rectangle
            RoundRectangle(objG, objP, 100, 200, 200, 100, 10);
        }      

        public void RoundRectangle(Graphics objG, Pen objP, float h, float v, float width, float height, float radius)
{
GraphicsPath objGP = new GraphicsPath();
objGP.AddLine(h + radius, v, h + width - (radius * 2), v);
objGP.AddArc(h + width - (radius * 2), v, radius * 2, radius * 2, 270, 90);
objGP.AddLine(h + width, v + radius, h + width, v + height - (radius * 2));
objGP.AddArc(h + width - (radius * 2), v + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Corner
            objGP.AddLine(h + width - (radius * 2), v + height, h + radius, v + height);
            objGP.AddArc(h, v + height - (radius * 2), radius * 2, radius * 2, 90, 90);
            objGP.AddLine(h, v + height - (radius * 2), h, v + radius);
            objGP.AddArc(h, v, radius * 2, radius * 2, 180, 90);
            objGP.CloseFigure();
            objG.DrawPath(objP, objGP);
             objGP.Dispose();
        }
    }
}

Output:



Regards,
Megha
Popularity  (5829 Views)
Create New Account
Article Discussion: C# and GDI : draw Round rectangles, fonts, eclipse, fonts
Perry posted at Saturday, October 25, 2008 6:09 AM
reply
eliza replied to Perry at Monday, November 15, 2010 10:38 PM
We all know Math.Round() basically rounds a particular decimal value. But if we look into this method closely it rounds the value to the nearest even value.
For example if we do Math.Round(2.5,0) it will return 2. But we may expect 3 intead of 2. In that case we can use MidPointRounding parameter of
Math.Round() method.
 
Math.Round can also take a parameter known as MidPointRoundingwhich helps us to select the Rounding option. That means whether we want to round towards even number or to the number away from zero.
 
For example, when someone  do the rounding of 8.5 .  MidpointRounding will help to specify whether rounding will be towards the even number (8) or it will be away from zero (9).
 
reply