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.
Here's the demo:
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)
You want to fill or paint a rectangle with some color. Well first you need to instantiate the
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)
SolidBrush instance we've used:
Dim brush As SolidBrush= New SolidBrush(Color.LightGray)
You can use the
DrawRectangle() method to draw a rectange on a windows form object. For that you need to pass:
Dim p As Pen = New Pen(cP, 6).
Ycordinates of the rectangle.
Heightof the rectangle.
mGraphics.DrawRectangle(p, 4, 4, 520, 440)
To draw an image onto the
Form we can use the
DrawImage class. This class is responsible for taking an
Image object, the
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)
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
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)
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