VB.NET Graphics - Draw String Into Image


VB.NET Graphics Example.

In this VB.NET piece we want to look at Graphics class and how to use it to achieve some common things we wouldn't normally achieve with simple windows forms alone. For example we want to see how we can load an image and draw some string into it. Something like this normally you do with a powerful image editor. However we want to see how to do this programmatically.

Demo

Here's the demo:

VB.NET Draw String into Image

Video Tutorial

Main Things We Learn

1. How to Load Image From File

We have an image in the File system. And you are supposed to load it into an Image object. Well first you need the image path:

        Private Const path As String = "D:Resources\Imgs\centaurus.jpg"

Then you have a reference to the Image class maybe as an instance field:

        Private image As Image

Then here's how we load it:

            image = image.FromFile(path)
2. How to Fill a rectangle

You want to fill or paint a rectangle with some color. Well first you need to instantiate the Graphics class:

            Dim mGraphics As Graphics = Graphics.FromHwnd(Handle)

Then you can use the FillRectangle() method. In it we pass a SolidBrush object, the X cordinate, the Y cordinate, the width and the height respectively.

            mGraphics.FillRectangle(brush, 4, 4, 520, 440)

Here's the SolidBrush instance we've used:

            Dim brush As SolidBrush= New SolidBrush(Color.LightGray)
2. How to Draw a Rectangle

You can use the DrawRectangle() method to draw a rectange on a windows form object. For that you need to pass:

  1. A Pen object e.g Dim p As Pen = New Pen(cP, 6).
  2. X and Y cordinates of the rectangle.
  3. Width and Height of the rectangle.
            mGraphics.DrawRectangle(p, 4, 4, 520, 440)
3. How to Draw an Image

To draw an image onto the Form we can use the DrawImage class. This class is responsible for taking an Image object, the x and y cordinates for the drawing, as well as the witdth and height respectively and drawing them on the form.

            mGraphics.DrawImage(image, 33, 35, 400, 290)
4. How to Draw a String Onto Image.

Well suppose you have now loaded your image via the Image class, you have already drawn the Image onto the Form, and now you want to draw a string onto the image. How do you do it?

Well you use the DrawString() method of the Graphics class.

In it you will pass the string to draw, the font to use, the SolidBrush as well as the width and height of the drawing.

            mGraphics.DrawString("Centaurus", font, blackBrush, 90, 95)

Program.vb

Here's the full source code.

Imports System.Drawing
Imports System.Windows.Forms

Module Program
    Friend Class Program
        Inherits Form

        Private p As Pen
        Private brush As SolidBrush
        Private ReadOnly blackBrush As SolidBrush = New SolidBrush(Color.White)
        Private Const path As String = "D:Resources\Imgs\centaurus.jpg"
        Private image As Image
        Private mFont As Font

        Private Sub InitializeComponent()
            SuspendLayout()
            ClientSize = New Size(580, 520)
            Text = "Centaurus"
            ResumeLayout(False)
        End Sub

        Private Sub initializeStuff()
            Dim cP As Color = Color.Gray
            Dim cB As Color = Color.LightGray
            p = New Pen(cP, 6)
            brush = New SolidBrush(cB)
            image = image.FromFile(path)
            font = New Font(New FontFamily("Times New Roman"), 20)
        End Sub

        Private Sub Sketch()
            Dim mGraphics As Graphics = Graphics.FromHwnd(Handle)
            mGraphics.FillRectangle(brush, 4, 4, 520, 440)
            mGraphics.DrawRectangle(p, 4, 4, 520, 440)
            mGraphics.DrawImage(image, 33, 35, 400, 290)
            mGraphics.DrawString("Centaurus", font, blackBrush, 90, 95)
            mGraphics.Dispose()
        End Sub

        Protected Overrides Sub OnPaint(ByVal pea As PaintEventArgs)
            Sketch()
        End Sub

        Public Sub New()
            InitializeComponent()
            initializeStuff()
        End Sub

    End Class
    Sub Main()
        Application.Run(New Program())
    End Sub

End Module

How to Download and Run

  1. Create a Console application in your Visual studio, yes Console application.
  2. Copy paste the code above into Program.cs file.
  3. That's it. Now just add references to windows forms and system.drawing into you project and run.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think


Previous Post