+ Reply to Thread
Results 1 to 3 of 3

Thread: Graphics in VB.NET Part 4 - Strings

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    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.

    Graphics in VB.NET
    1. Bitmaps
    2. Graphics
    3. Figures
    4. Strings







    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:
    Graphics in VB.NET Part 4 - Strings-text.jpg



    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
    Last edited by Vswe; 11-07-2009 at 02:44 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Graphics in VB.NET Part 4 - Strings

    Nicely done! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Graphics in VB.NET Part 4 - Strings

    And a good finish +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Graphics in VB.NET Part 3 - Figures
    By Vswe in forum Visual Basic Tutorials
    Replies: 5
    Last Post: 01-13-2010, 12:01 PM
  2. Graphics in VB.NET Part 2 - Graphics
    By Vswe in forum Visual Basic Tutorials
    Replies: 4
    Last Post: 11-07-2009, 06:03 PM
  3. Graphics in VB.NET Part 1 - Bitmaps
    By Vswe in forum Visual Basic Tutorials
    Replies: 3
    Last Post: 11-07-2009, 05:59 PM
  4. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  5. Help with a part of code - working with strings
    By Joeyeti in forum JavaScript and CSS
    Replies: 2
    Last Post: 11-27-2008, 01:12 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts