+ Reply to Thread
Results 1 to 6 of 6

Thread: Graphics in VB.NET Part 3 - Figures

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

    Graphics in VB.NET Part 3 - Figures

    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





    Fill figures

    By using graphics you can use "Fill..." to fill areas with a selected color. The first parameter is which brush you will use. The brush is which color you want to use. Then it's different on different "Fills" how you define the coordinates and size of the area you want to fill. The different possible types of areas are:

    Code:
    FillClosedCurve
    FillEllipse
    FillPath
    FillPie
    FillPolygon
    FillPolygon
    FillPolygon
    FillRectangle
    FillRectangles
    FillRegion
    If you want to paint the whole background in one color it's probably best to use FillRectangle, I will use that as an example on how to use the "Fills":


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

    Dim g As Graphics = Graphics.FromImage(Img)
    g.FillRectangle(Brushes.Blue, New Rectangle(0, 0, Img.Width, Img.Height))
    g.Dispose()[/highlight]

    In the above example we're familiar with everything expect this line:

    [highlight=VB.NET] g.FillRectangle(Brushes.Blue, New Rectangle(0, 0, Img.Width, Img.Height))[/highlight]




    What it's do is that it fills a rectangle

    Code:
           g.FillRectangle(Brushes.Blue, New Rectangle(0, 0, Img.Width, Img.Height))
    with the color blue

    Code:
           g.FillRectangle(Brushes.Blue, New Rectangle(0, 0, Img.Width, Img.Height))
    on the coordinates of a new rectangle.

    Code:
           g.FillRectangle(Brushes.Blue, New Rectangle(0, 0, 400, 400))




    The rectangle starts at the top left corner

    Code:
    New Rectangle(0, 0, Img.Width, Img.Height)
    and has the same size as the Image.

    Code:
    New Rectangle(0, 0, Img.Width, Img.Height)



    All types in the above list works pretty much the same, so by using this you can draw filled forms to images. A good reason to use it is to create a background color as showed above as an example. An example on more then just filling the background can be viewed here below by I by this code:


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

    Dim g As Graphics = Graphics.FromImage(Img)
    g.FillRectangle(Brushes.Blue, New Rectangle(0, 0, Img.Width, Img.Height))
    g.FillEllipse(Brushes.Yellow, New RectangleF(25, 25, 350, 350))
    g.FillEllipse(Brushes.Black, New RectangleF(100, 75, 50, 50))
    g.FillEllipse(Brushes.Black, New RectangleF(250, 75, 50, 50))
    g.FillPie(Brushes.Black, New Rectangle(100, 150, 200, 200), 0, 180)
    g.Dispose()[/highlight]


    Creates this image:


    Graphics in VB.NET Part 3 - Figures-smiley.jpg




    Drawing figures

    There also exists "Draw...", here we have few different types of them. We have 3 types DrawImage(for bitmaps), 2 types of DrawIcon(for icons) and DrawString(which is for strings) and then the rest is figures as the "Fills" were. However there's a lot more "Draw figures" then "Fill figures". The other different is that draw only draws the edge of the figure instead of filling it, this also means you have to replace brushes with pens, for example:

    Code:
    Pens.Blue
    instead of
    Code:
    Brushes.Blue
    To see the difference I've "converted" the above smiley example to use "Draws", like so:

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

    Dim g As Graphics = Graphics.FromImage(Img)
    'I had to do the background white to be able to see the iamge
    g.FillRectangle(Brushes.White, New Rectangle(0, 0, Img.Width, Img.Height))

    g.DrawRectangle(Pens.Blue, New Rectangle(0, 0, Img.Width, Img.Height))
    g.DrawEllipse(Pens.Yellow, New RectangleF(25, 25, 350, 350))
    g.DrawEllipse(Pens.Black, New RectangleF(100, 75, 50, 50))
    g.DrawEllipse(Pens.Black, New RectangleF(250, 75, 50, 50))
    g.DrawPie(Pens.Black, New Rectangle(100, 150, 200, 200), 0, 180)
    g.Dispose()[/highlight]

    Which then gives us this result:

    Graphics in VB.NET Part 3 - Figures-smiley2.jpg
    Note that I added one "Fill" to give the image a white background so we could see what it drew.


    That was everything for this part, in the next one I'll show you how to draw a string to an image using graphics and DrawString.
    Last edited by Vswe; 11-07-2009 at 02:45 PM.

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

     
  3. #2
    Jordan Guest

    Re: Graphics in VB.NET Part 3 - Figures

    You created a smilie just like Chilies! Nice work. +rep

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

    Re: Graphics in VB.NET Part 3 - Figures

    Very cool! +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Graphics in VB.NET Part 3 - Figures

    Very cool! Nice drawing. What does DrawPie do?

    Your VB smilies can't beat my ActionScript smilies.

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

    Re: Graphics in VB.NET Part 3 - Figures

    It draws a half circle(the mouth). I didn't try to do it better, it was just an example

  7. #6
    arkanion is offline Newbie
    Join Date
    Jan 2010
    Posts
    10
    Rep Power
    0

    Re: Graphics in VB.NET Part 3 - Figures

    i love VB

+ 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. Action Figures eCommerce Website Available For Sale
    By Zethix in forum Sites for Sale
    Replies: 0
    Last Post: 05-31-2011, 05:24 AM
  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. Graphics in VB.NET Part 4 - Strings
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 11-07-2009, 05:31 PM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 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