+ Reply to Thread
Results 1 to 6 of 6

Thread: Graphics in VB.NET Part 3 - Figures

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,784
    Blog Entries
    5

    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 04:45 PM.

  2. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Graphics in VB.NET Part 3 - Figures

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

  3. #3
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,680
    Blog Entries
    57

    Re: Graphics in VB.NET Part 3 - Figures

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

  4. #4
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  5. #5
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,784
    Blog Entries
    5

    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

  6. #6
    Newbie arkanion is an unknown quantity at this point
    Join Date
    Jan 2010
    Posts
    10

    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. Graphics in VB.NET Part 2 - Graphics
    By Vswe in forum VB Tutorials
    Replies: 4
    Last Post: 11-07-2009, 08:03 PM
  2. Graphics in VB.NET Part 1 - Bitmaps
    By Vswe in forum VB Tutorials
    Replies: 3
    Last Post: 11-07-2009, 07:59 PM
  3. Replies: 3
    Last Post: 11-02-2009, 07:40 PM
  4. Replies: 2
    Last Post: 11-02-2009, 08:08 AM
  5. Replies: 2
    Last Post: 11-02-2009, 07:37 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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