Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake bitmap files containing numbers in circles in Visual Basic 2005
DescriptionThis example shows how to make bitmap files containing numbers in circles in Visual Basic 2005.
Keywordsgraphics, bitmap files, VB.NET, Visual Basic 2005
CategoriesGraphics, VB.NET
 
Sometimes I need small images of numbers in circles to annotate an image for a book or article so I wrote this program to make them. It gives each a blue gradient background and saves them in separate files. You can use a similar technique to make files containing other graphics if you need them.

When you click the program's button, the following code loops through the numbers 0 through 9. For each it creates a new Bitmap and an associated Graphics object.

The program sets the Graphics object's TextRenderingHint property to the text is drawn smoothly. It also sets the SmoothingMode property so the circle is drawn smoothly.

Next the code fills an ellipse in the Bitmap with the color gradient (change the brush to use a different background) and draws the number centered in it. Finally the code saves the Bitmap into a file.

 
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    Const WID As Integer = 24

    Dim base_path As String = txtDirectory.Text
    If Not base_path.EndsWith("\") Then base_path &= "\"
    Dim the_font As New Font("Arial", 10, FontStyle.Bold)

    For i As Integer = 0 To 9
        Dim bm As New Bitmap(WID, WID)
        Dim gr As Graphics = Graphics.FromImage(bm)
        gr.TextRenderingHint = _
            Drawing.Text.TextRenderingHint.AntiAliasGridFit
        'gr.InterpolationMode =
        ' Drawing2D.InterpolationMode.Bicubic
        gr.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
        gr.Clear(Color.White)
        Dim layout_rectangle As New Rectangle(0, 0, WID - _
            1, WID - 1)
        Using br As New _
            LinearGradientBrush(layout_rectangle, _
         Color.White, Color.Blue, _
             LinearGradientMode.ForwardDiagonal)
            gr.FillEllipse(br, layout_rectangle)
            gr.DrawEllipse(Pens.Blue, layout_rectangle)
        End Using
        Dim string_format As New StringFormat()
        string_format.LineAlignment = StringAlignment.Center
        string_format.Alignment = StringAlignment.Center
        gr.DrawString(i.ToString, the_font, Brushes.White, _
            layout_rectangle, string_format)
        bm.Save(base_path & i.ToString & ".bmp", _
            System.Drawing.Imaging.ImageFormat.Bmp)
    Next i

    MessageBox.Show("Done")
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated