VB.Net Tutorial -Drawing libraries
‘System.Drawing’ can be one of the fun working libraries for working on interesting tasks like imaging. This is loaded with a lot of features that can make the drawing and imaging activities much easier.
Dealing with Bitmap
I usually prefer to work with bitmap when I am working on the imaging. The reason behind is, it has a lot of properties and methods to work with. We can easily save, change the pixel, and retrieve the pixel.
Drawing Texts
Just like drawing the lines, pixels, circles on an image, it is very easy to write a text on the image using graphic object’s DrawString() method. This graphic object has to be based on the image where we are going to write to. So use the FromImage function of the Graphics to get the graphics object of the image.
Formatting with Graphics
Graphics object can be used to format the image, text. So use the properly suited overloaded method to handle the required formats using font, brush, location. I demonstrated how to use brush, and font in a very simple manner. Though you can do bold, italics, colorful, I will leave it to you to discover.
Source Code
Designer(*.Designer.vb)
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.Button1 = New System.Windows.Forms.Button
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.Location = New System.Drawing.Point(0, 0)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(391, 223)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(0, 229)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Load Image"
Me.Button1.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(398, 269)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents Button1 As System.Windows.Forms.Button
End Class
Code Behind(*.vb)
Imports System.Drawing.Imaging
Imports System.Drawing
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Image = DrawText("Hi")
End Sub
Private Function DrawText(ByVal Text As String) As Bitmap
Dim TextBitmap As New Bitmap(100, 100)
Dim Brush As New SolidBrush(Color.Blue)
Dim SelectedFont = New Font(FontFamily.GenericSerif, 40)
Using Graphic = Graphics.FromImage(TextBitmap)
Graphic.DrawString(Text, SelectedFont, Brush, 0, 0)
End Using
Return TextBitmap
End Function
End Class