Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Draw Text on image using Solid Brush in VB and C #.net.

Author: Maliha Atteeq
Download Source Code : 1084_code.zip

In this simple article you will learn that how to draw text on image using Solid Brush in VB and C #.net.

 Graphics:

While playing with the graphics you must keep the following point in your mind:

You can’t draw graphics on simple load event of the form. To create graphics on the form, you must override the onpaint() function of the form.

The syntax of the onpaint() function is:

C#:

protected override void OnPaint (PaintEventArgs e)

{

}

VB:

Protected Overrides Sub OnPaint(ByVal As PaintEventArgs)
        
    
End Sub

In this article I will tell you that how can we draw a string on an image using solid brush.

Solid Brush:

Like a pen, the primary characteristic of a brush is its color. To help you create a simple brush, the System::Drawing namespace provides the static sealed Brushes class. The only feature this class provides is the ability to specify a color to use on a brush. As a static class, you never have to instantiate it. To create a simple brush whose only information is provided by its color, call the Brushes class and access a color by qualifying it with the name of the class. Each color is provided by its name as a property.

C#:

Brush brush = new SolidBrush(Color.OliveDrab);

VB:

Dim brush As Brush = New SolidBrush(Color.OliveDrab)

 

In order to display the graphics you have to override the function onpaint(). Create a graphics and a rectangle object. Also create an object of Solidbrush class, this class can’t be inherited.

You can create the object of font class by using this constructor:

C#:

Font fontBold = new Font("Times New Roman", 30, FontStyle.Bold);

VB:                                                                 

Dim fontBold As New Font("Times New Roman", 30, FontStyle.Bold)

the next function is the drawString(). This function has five overloaded methods,depending ob your requirements you can use any of them. The return type of this function is void.

The syntax of the Drawstring() function is:

C#:

g.DrawString("SolidBrush", f, hb, 0, 0);

VB:

g.DrawString("SolidBrush", f, hb, 0, 0)

To demonstrate make a new window application and write the following code on form’s onpaint event:

C#:

protected override void  OnPaint(PaintEventArgs e)

        {

            Graphics g=e.Graphics;

            string str = "My Solid brush application";

            Brush brush = new SolidBrush(Color.OliveDrab);

            Font fontBold = new Font("Times New Roman", 30, FontStyle.Bold);

            PointF ptf = new PointF(50,50);

            StringFormat strfmt = StringFormat.GenericTypographic;

            strfmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;

            g.DrawString(str, fontBold, brush, ptf, strfmt);

            

        }         

VB:

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

    Dim g As Graphics = e.Graphics

    Dim str As String = "My Solid brush application"

    Dim brush As Brush = New SolidBrush(Color.OliveDrab)

    Dim fontBold As New Font("Times New Roman", 30, FontStyle.Bold)

    Dim ptf As New PointF(50, 50)

    Dim strfmt As StringFormat = StringFormat.GenericTypographic

    strfmt.FormatFlags = strfmt.FormatFlags Or StringFormatFlags.MeasureTrailingSpaces

    g.DrawString(str, fontBold, brush, ptf, strfmt)

 

End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

                       this.Text = "DEVASP  APPLICATION";      

        }

Vb:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

  Me.Text = "DEVASP  APPLICATION"

This simple article tells that how to draw text on image using Solid Brush in VB and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Fill ellipse with image in VB - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net