Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Graphics in VB.NET Part 4 - Strings

VB.NET form string

  • Please log in to reply
3 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 07 November 2009 - 01:07 PM

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.

Graphics in VB.NET








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:

        Dim myFont As Font = New Font("Times new roman", 28, FontStyle.Bold, GraphicsUnit.Point)

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:


        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)

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:
[ATTACH]2299[/ATTACH]



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:


        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)


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

Attached Thumbnails

  • Text.jpg

Edited by Vswe, 07 November 2009 - 02:44 PM.

  • 2

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 07 November 2009 - 02:36 PM

Nicely done! +rep
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 November 2009 - 05:31 PM

And a good finish :) +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 moore1985

moore1985

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 24 August 2013 - 04:28 AM

Gone though all 4 parts, very helpful to a dummy like me, even it is 4 years old post.

 

 

It is never too late to learn. :thumbup:

 

 


  • 0