Archive

Headline

Sharing Information

Visitor

free counters

Trafict & Ranking

SEO Stats powered by MyPagerank.Net
Powered by  MyPagerank.Net Yahoo bot last visit powered by MyPagerank.Net Msn bot last visit powered by MyPagerank.Net

Tutorial : Graphics in VB.NET Part 4 - Strings

In this 4 parts long tutorial series I will introduce you to graphics in VB.NET. The four parts is meant to be in order so I recomend you to read them in order to be able to understand the later parts.

Draw a String
We can use the Graphics together with DrawString to draw a string on our image, to do this we need to enter the parameters(of course there's options here but I'll show this way): String, Font, Brush Color, X-position and Y-position. The thing that maybe is unknown for you is the font type. 


One way to create a new font is to use font family, size, font style and unit. Like so:

[highlight=
VB.NET] Dim myFont As Font = New Font("Times new roman", 28, FontStyle.Bold, GraphicsUnit.Point)[/highlight]
The font family is here Times new roman, the size is 28 in the unit point(last parameter) and the text will also be bold.

Back to DrawString, the example below will make the background white and print "Message to write" in black on the background:


[highlight=
VB.NET] Dim Img As New Bitmap(400, 400)
Dim g As Graphics = Graphics.FromImage(Img)

g.FillRectangle(Brushes.White, New Rectangle(0, 0, Img.Width, Img.Height))
Dim myString As String = "Message to write"
Dim myFont As Font = New Font("Times new roman", 28, FontStyle.Bold, GraphicsUnit.Point)
Dim myBrush As Brush = Brushes.Black
Dim X As Integer = 35
Dim Y As Integer = 35

g.DrawString(myString, myFont, myBrush, X, Y)
[/highlight]
The code is pretty simple to follow, first we declares an image and a graphics to it and paints the background white.
Then it's storing all required values in variables and then prints it out.


The result will look like this:


 


Strings' Pixel Length

If we want to write another string in relation to one string or want its last part to be at a certain position we need to know how the string will be when we'll print it on the image. To get the size we're using MeasureString to get the size of as a sizeF value. To use MeasureString we simply uses the string together with the font. The example below is the same as the one above with the difference that this time it will print the text in the middle by using MeasureString:


[highlight=
VB.NET] Dim Img As New Bitmap(400, 400)
Dim g As Graphics = Graphics.FromImage(Img)

g.FillRectangle(Brushes.White, New Rectangle(0, 0, Img.Width, Img.Height))

Dim myString As String = "Message to write"
Dim myFont As Font = New Font("Times new roman", 28, FontStyle.Bold, GraphicsUnit.Point)
Dim myBrush As Brush = Brushes.Black
Dim stringSize As SizeF = g.MeasureString(myString, myFont)

Dim X As Integer = (Img.Width - stringSize.Width) / 2
Dim Y As Integer = (Img.Height - stringSize.Height) / 2

g.DrawString(myString, myFont, myBrush, X, Y)[/highlight]

Notice how I get the size of the string and them take the half difference to the image's size to be the string's position.

This was everything for this 4 part long tutorial series about graphics in VB.NET. Now you should be able to resize your images in the right size, put more images together, change the background color, draw filled and unfilled figures to an image, draw strings to an image where you want it, save an image in different formats etc. Hope you have had fun learning

0 komentar:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Translate

Subscribe Here

Enter your email address:

Like Facebook

Online Chat Comment

Reference

Stats and info

Top Referrer