25.1.11 Project 1 - Image and text

This project shows you how to add text to an image using methods of the Cairo class:

B1

Figure 25.1.11.1: Original

The source code for project 1 is very short:

[1] ' Gambas class file
[2] 
[3] Public hImage As Image
[4] 
[5] Public Sub Form_Open()
[6] ' Create an image
[7]   hImage = Image.Load(Application.Path &/ "images/augentrost.jpg")
[8]   
[9] ' Scale programme window appropriately to the image size
[10]   FMain.W = hImage.W + 2 * Desktop.Scale
[11]   FMain.H = hImage.H + 2 * Desktop.Scale
[12]   FMain.Center()
[13]   FMain.Resizable = False
[14]   
[15]   DrawToImage()
[16] 
[17] End
[18] 
[19] Public Sub DrawToImage()
[20]   Dim sText1, sText2 As String
[21]     
[22]   Cairo.Begin(hImage)
[23]   ' Cairo.Rotate(Pi / 2) '-- Alternative: Rotate text by a specific angle
[24]   ' Draw texts on the image object  
[25]     Cairo.MoveTo(10, 260)
[26]     Cairo.Font.Name = "Sans Serif" '-- Draw texts on the image object
[27]     Cairo.Font.Size = 18.5
[28]     Cairo.Font.Bold = True
[29]     Cairo.Source = Cairo.ColorPattern(Color.Yellow)
[30]     sText1 = "FLORA DER ALPEN"
[31]     Cairo.DrawText(sText1)
[32]   ' -----------------------------------------------------------------  
[33]     sText2 = "Augentrost (Euphrasia minima) - Sonthofen - Juli 2012"
[34]     Cairo.MoveTo(10, 260 + Cairo.TextExtents(sText2).Height + 4)
[35]     Cairo.Font.Size = 13.3
[36]     Cairo.Font.Bold = False
[37]     Cairo.Font.Slant = Cairo.FontSlantNormal
[38]     Cairo.Source = Cairo.ColorPattern(Color.White)
[39]     Cairo.DrawText(sText2)
[40]   Cairo.End()
[41]   
[42] End
[43] 
[44] Public Sub dwgArea_Draw()
[45] ' Display of the image: Insert image into the DrawingArea with a Paint method
[46]   Paint.DrawImage(hImage, 0, 0, hImage.W, hImage.H)
[47] ' Save image
[48]   hImage.Save(Application.Path &/ "images/image.png")
[49] End

Comment:

Alternatively, you can also add an open and save dialogue for the image and read in the texts using suitable components.

The result is impressive, as the dominant colours from the image have been used for the font colours:

B2

Figure 25.1.11.2: Image with text

Download

Chapter & Projects

Download