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

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
By Perry    Popularity  (8327 Views)