logo

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

By Raj Cool...
Printer Friendly Version
View My Articles
1118 Views
    

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



Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Article Discussion: C# and GDI : draw Round rectangles, fonts, eclipse, fonts
Raj Cool... posted at Saturday, October 25, 2008 6:09 AM
Original Article
 






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010