23.3.5.5 Image shadow

This project centres on the display of an image to which an image shadow is added. The original image should fulfil certain requirements - such as a transparent background - so that the addition of the image shadow provides a good effect.

B1

Figure 23.3.5.5.1: Original image

B2

Figure 23.3.5.5.2: Original image with shadow

The shadow effect can be seen most clearly if you start the project and look at both images one after the other.

The first project uses the (partial) programme 'convert' from the programme collection 'ImageMagick', which must therefore be installed on your system. You can install the programme and its documentation under Ubuntu using this instruction:

sudo apt-get install imagemagick imagemagick-doc 

If you do not want to use the external programme, there is another project in the second section that only uses Gambas methods.

Source code 1:

[1] ' Gambas class file
[2] 
[3] Public Sub Form_Open()
[4]    Me.Center
[5]    Me.Resizable = False
[6]    picBox.Picture = Picture[Application.Path & "/logo.png"]
[7]    If Exist(Application.Path & "/logos.png") Then
[8]       Kill Application.Path & "/logos.png"
[9]    Endif
[10]    btnGenerateShadow.Text = "Schatten ein ..."
[11] End
[12] 
[13] Public Sub btnGenerateShadow_Click()
[14]   Dim sCommand As String
[15] 
[16] ' The 'convert' programme must be installed on the system
[17]   sCommand = "convert -background gray -shadow 80x3 " & Application.path & "/logo.png " 
                & Application.path & "/logos.png"
[18]   Shell sCommand Wait
[19]   MakeShadow()
[20] 
[21] End
[22] 
[23] Public Sub MakeShadow()
[24]   Dim Foreground, Shadow, hImage As Image 
[25] 
[26]   Shadow = Image.Load(Application.path & "/logos.png")
[27]   Foreground = Image.Load(Application.path & "/logo.png")
[28]    
[29]   If btnGenerateShadow.Text = "Schatten ein ..." Then
[30]      hImage = Shadow.Copy()
[31]      hImage.PaintImage(Foreground, 0, 0)
[32]      btnGenerateShadow.Text = "Schatten aus ..."
[33]   Else
[34]      hImage = Foreground.Copy()
[35]      btnGenerateShadow.Text = "Schatten ein ..."
[36]   Endif 
[37]    
[38]   picBox.Picture = hImage.Picture
[39]   
[40] End

Comment:

In the following variant by Tobias Boege and Ingo Beckert, only Gambas methods are used to create the same shadow effect.

B3 B4

Figures 23.3.5.5.3: Original image and image with shadow

Source code 2 for the alternative project is also shown here in full:

[1] ' Gambas class file
[2] 
[3] Public Sub Form_Open()
[4]   Me.Center
[5]   Me.Resizable = False
[6]   picBox.Picture = Picture[Application.Path & "/logo.png"]
[7]   btnGenerateShadow.Text = "Schatten ein ..."
[8] End
[9] 
[10] Public Sub btnGenerateShadow_Click()
[11]   MakeShadow
[12] End
[13] 
[14] Public Sub MakeShadow()
[15]   Dim Foreground, hImage As Image 
[16]    
[17]   Foreground = Image.Load(Application.path & "/logo.png")
[18]    
[19]   If btnGenerateShadow.Text = "Schatten ein ..." Then
[20]      hImage = Foreground.Copy()
[21]      hImage.PaintImage(Foreground, 0, 0)
[22]      picBox.Picture = AddShadow(picBox.Picture.Image).Picture
[23]      btnGenerateShadow.Text = "Schatten aus ..."
[24]   Else
[25]      hImage = Foreground.Copy()
[26]      picBox.Picture = hImage.Picture
[27]      btnGenerateShadow.Text = "Schatten ein ..."
[28]   Endif 
[29]    
[30] End
[31] 
[32] Public Function AddShadow(hImage As Image) As Image
[33]   Dim hNewImage As New Image(hImage.W, hImage.H)
[34] 
[35]   Paint.Begin(hNewImage)
[36]     Paint.DrawImage(Shadow(hImage), 4, 4)
[37]     Paint.DrawImage(hImage, 0, 0)
[38]   Paint.End()   
[39]   Return hNewImage
[40]    
[41] End
[42] 
[43] Public Function Shadow(hImage As Image) As Image
[44]   Dim hShadow As Image = hImage.Copy()
[45] 
[46]   hShadow.Fuzzy(3)
[47]   hShadow.Colorize(Color.Gray)
[48]   hShadow.Opacity(0.5)
[49]   Return hShadow
[50]   
[51] End

Download