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
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Text rendering and single bit per pixel grid fit in VB - C #.net.

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

In this simple article you will learn that how to use text rendering and single bit per pixel grid fit 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 form using solid brush and rectangle.

Text rendering hints:

You can use any graphics object as a surface to render text. Rendering text requires a Brush object, which indicates what pattern will fill the text, and a Font object, which describes the pattern to be filled. The font can be any named font that is installed on the system, and the brush can be any kind of brush. Thus, it is possible to paint text using a solid color, a pattern, or even an image.

For text rendering you have to specify the type or mode of the rendering. In this article I use the mode Single bit per pixel grid fit.

C#:

e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

VB:

e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit

In order to display the graphics you have to override the function onpaint(). Create a graphics and a rectangle object. You can create the object of pen class by using this constructor:

C#:

Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic);

VB:

Dim TextFont As New Font("Times New Roman", 25, FontStyle.Italic)

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#:

       private void SmoothingText_Paint(object sender, PaintEventArgs e)

        {

            Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic);

            e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

            e.Graphics.DrawString("My text rendering application", TextFont, Brushes.Cyan, 20, 20);

 

        }

          

VB:

Private Sub SmoothingText_Paint(ByVal sender As Object, ByVal e As PaintEventArgs)

    Dim TextFont As New Font("Times New Roman", 25, FontStyle.Italic)

    e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit

    e.Graphics.DrawString("My text rendering application", TextFont, Brushes.Cyan, 20, 20)

 

End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP APPLICATION";

            this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingText_Paint);

            this.ResumeLayout(false);

        }

VB:

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

    Me.Text = "DEVASP APPLICATION"

    Me.Paint += New system.Windows.Forms.PaintEventHandler(Me.SmoothingText_Paint)

    Me.ResumeLayout(False)

End Sub

This simple article tells that how to use text rendering and single bit per pixel grid fit in vb and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Use Solid Brush and rectangle to draw text on form in VB - C #.net.

Disclaimer - Privacy
© 2002-2014 DevASP.net