The Gambas GUI toolkits QT and GTK implement gb.image.io, so it is not necessary to activate this component in the project properties if you want to create a GUI application. You only need to add gb.image.io if the application does not use a GUI toolkit, so that all properties and methods from gb.image.io methods are available to you.
This class is a new implementation of Image in gb.image. All image formats provided by the gdk-pixbuf library are supported (at least JPEG, PNG, BMP, GIF and XPM). The class can be created and works like a read and write array. The new implementation of Image in gb.image.io has been extended by 4 methods compared to Image (gb.image).
The Image class (gb.image.io) has these newly implemented methods:
Method | Return type | Description |
---|---|---|
Static Function FromString ( Data As String ) | Image | Loads an image from the content of a string. |
Static Function Load ( Path As String ) | Image | Loads an image from a file. The Path parameter is the file path. |
Sub Save ( Path As String [ , Quality As Integer ] ) | - | Saves the image in a file. The file extension of Path specifies the format of the saved image. The Quality argument is only used if an image is saved in jpeg or png format. |
Function Stretch ( Width As Integer, Height As Integer ) | Image | The function changes the size of an image to the specified values for Width and Height, which does not apply to vector graphics. If Width or Height is negative, the image proportion is retained. |
Table 23.4.2.8.1 : Methods of the Image class
Notes:
The identifier for the function 'Stretch(…) is misleading. The function does not return a 'stretched version' of an image as a function value per se; only if a parameter - either Width or Height - is negative is the image proportion retained when the size is changed.
In this project, the two functions Load (Path As String) and Stretch (Width As Integer, Height As Integer) as well as the procedure Save (Path As String [ , Quality As Integer ]) are used.
You can use
This is the source code:
' Gambas class file Public hImage As New Image Public hImageMod As New Image Public sPath As String Public Sub Form_Open() FMain.Caption = "Image.Stretch" End Public Sub btnLoad_Click() '-- Show the original image in PictureBox1 hImage = Image.Load(Application.Path &/ "images/fractal.png") '-- Show the original image in PictureBox1 PictureBox1.Picture = hImage.Picture '-- Stretch ( Width As Integer, Height As Integer ) hImageMod = hImage.Stretch(spboxW.Value, spboxH.Value) '-- Display the modified image in the PictureBox2 PictureBox2.Picture = hImageMod.Picture End Public Sub spboxW_Change() PictureBox2.Picture = hImageMod.Stretch(spboxW.Value, spboxH.Value).Picture End Public Sub spboxH_Change() PictureBox2.Picture = hImageMod.Stretch(spboxW.Value, spboxH.Value).Picture End Public Sub btnSave_Click() If Not IsNull(hImageMod) Then hImageMod.Save(Application.Path &/ "images/fractal.stretch.png") End
When the programme is started, the start parameters for the Stretch(…) function are set to W = 150 and H = 200:
Figure 23.4.2.8.1: Display after starting the programme
The parameters were then set to W = 120 and H = 370. The image proportion is not retained:
Figure 23.4.2.8.2: Display with changed parameters
However, if you set one of the two parameters - in this case the image width W - to (any) negative value and then change the image height H, the image is set to the selected image height and the image width is (automatically) changed so that the image proportion is retained:
Figure 23.4.2.8.3: Display with a negative parameter
The project is available in the download area as a project archive.
Project