C# tutorial-C# drawing text


C# drawing text

In C#, text and other shapes can be drawn on a form or on other controls. To start drawing objects on the control, you need to create graphic object for the control. The graphic object is created by using CreateGraphics() method of the control. To draw string on the control, you can use the drawString() method of the graphic object.

Example: drawing text on the form

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace WindowsFormsApplication2

{

    public partial class Form2 : Form

    {

        public Form2()

        {

            InitializeComponent();

        }

     

        private void Form2_Paint(object sender, PaintEventArgs e)

        { //place the C# code about drawing here

            //Text to draw

            string textToDraw = "Drawing text in C#";

            //Create graphic object for the current form

            Graphics gs = this.CreateGraphics();

            //Create brush objec

            Brush br = new SolidBrush(Color.BlueViolet);

            //Create font object

            Font f = new Font("Courier New", 20);

            //Create point object

            PointF pf = new PointF(30, 100);

            PointF pf1 = new PointF(30, 140);

            //Then you are ready to draw the text on your form

            gs.DrawString(textToDraw, f, br, pf);

            gs.DrawString(textToDraw, f, br, pf1);

 

        }

    }

}

C# to draw text




Comments

CAPTCHA image




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.