Creating a gambas2 program, step by step, a telephone index

From : http://listingambas.blogspot.com/2011/06/anexo-2-convert-miniaturas-en-nuestro.html

Annex 2: convert, thumbnails in our gridViewData

In our gridviewData, we see that we can' t display the whole pictures. How about making small thumbnails of them that  we may see better ... .????
Linux has commands and free programs to do so ....
Let's work:

The program convert help us to reduce the required size images uploaded to our gridviews,

Installation:

From the console prompt, type "convert". If it can't be found, it would install the program:
$ Convert
The program "convert" can be found in the following packages:
* Imagemagick
* Graphicsmagick-imagemagick-compat
Try: sudo apt-get install <paquete selected>
bash: convert: command not found
$ Sudo apt-get install imagemagick

We are asked  to confirm installation of the package, and it install itself.

Use of convert in our program: the SHELL command

Once the program installed , we include the code to do:
$ Convert-size 120x120-resize 120x120 + lena512.bmp profile '*' thumbnailDD.jpg

d Thus, "-size 120x120" gives a hint to the JPEG decoder that the image will be downscaled to 120x120, allowing it to run faster, preventing it to return a high-resolution image. The "-resize 120x120" specifies the desired dimensions of the output image. It will be scaled so its largest dimension is 120 pixiles. The "*" in "+ profile" removes any ICM, EXIF, IPTC, or other profiles iformation that may be held in the input and ise not desired in the thumbnails. "

Running the image  "lena512.bmp" need 257.1 kib; we coverted it to the so called "thumbnailDD.jpg" of only 3.7 kib.

Well, and as we code it in  gambas2, we use the command "SHELL".
We will create a new module called thumbnails, and put in it this code:

public function DoIt (file AS String , size AS StringAS string
DIM Proc AS process
DIM sline AS String
DIM mini AS String
'Check if Exist a directory named mini
IF Exist ( " ~ / listin / mini " ) THEN
  '   directory mini Exists, no  need to create
ELSE
  ' directory mini does not exists, we must create it ...
  Mkdir " ~ / listin "
  'And then " mini "
  Mkdir " ~ / listin / mini "
ENDIF
'Note:
'Command: file.dir (path), extract only the path name (without file name)
'Command: file.name (path), extract only the file name (without path)
mini = " ~ / listin / mini " & "/ mini" & file . name ( file )
IF Exist ( mini ) Then
  '  file miniExists , do not have to create it
  RETURN mini
  ELSE
  'Size can be 120x120, 96x96, etc.
  line = " convert -size " & size & "" & file & "-resize" & size & "+ profile '*'" & mini
  proc = SHELL line WAIT
  'Execute the process and control the output flow of your application 
  return mini
ENDIF
END

Notes:
1 - The function Thumnais.DoIt () in addition to resizing the image  returns the path where it is created.
2 - " ~ / l ": the character ~ is replaced by the user's home directory, regardless of which user it is "/ home / July" or "/ home / john".

3 - Exist () :  If does not Exists ( " ~ / listin / mini " ), we create a directory called listin / mini from our home / user, where we store all the thumbnails made. If file exists, we will not run convert

4. SHELL , this command runs from the command line the program convert with all the attributes that we added (file path, size, and new name for the thumbnail)

Well, once developed the code and created the new module, we will create a checkbox (checkboxmini) in the form Fmain providing the user to "check or uncheck" the option use of thumbnails in gridviewData , and thus we also modify subroutine "title.fill ()" to display the thumbnails rather than original files.



In Fmain form, we need to code  that when the value of checkboxmini changes, we also have to run the subroutine Title.Fill ()
PUBLIC SUB CheckBoxmini_Click ()
  Title. Fill ()
END


And in the code Title.Fill () :
PUBLIC SUB Fill ()
DIM a AS Integer
FMain. GridViewData . Rows . COUNT = var. id . COUNT
FOR a = 0 TO var. id . COUNT - 1
    WITH FMain
        IF FMain. CheckBoxmini . Value = FALSE THEN
        . GridViewData . Row s . height = 25
        . GridViewData [a, 0 ]. Picture = Picture [var. picture [a]]
        ELSE
        . GridViewData [a, 0 ]. Alignment = 1
        . GridViewData . Row s . height = 100
        . GridViewData [a, 0 ]. Picture = Picture [Thumbnail. DoIt (var. photo [a], "96x96" )]
        ENDIF
        . GridViewData [a, 1 ]. text = var. name [a]
        . GridViewData [a, 2 ]. text = var. surname [a]
    ...    ...

Note: The yellow part is what we changed. It make that when checkboxmini.value = false  the normal picture is drawn, and when it is True, the thumbnail is produced and  the row height is sized to display it.

Important Note: We add the same code at any location where gridviewData is filled .
And in  filter module , we must make the same changes to .ConceptFilter:


    ...
    ...
valid = in (sLine, pattern) AND in (sLine, pattern1) AND in (sLine, pattern2)
IF valid THEN
    count  + = 1
    FMain. GridViewData . Rows . COUNT = count 
    WITH FMain
        IF FMain. CheckBoxmini . Value = FALSE THEN
        . GridViewData . Rows . height = 25
        . GridViewData [count  - 1 , 0 ]. Picture = Picture [var. picture [a]]
        ELSE
        . GridViewData [count  - 1 , 0 ]. Alignment = 1
        . GridViewData . Rows . height = 100
        . GridViewData [count  - 1 , 0 ]. Picture = Picture [thumbnails. doIt (var. photo [a], "96x96" )]
        ENDIF
        .GridViewData [count  - 1 , 1 ]. text = var. name [a]
    ...    ...


Here is an example of records with and without thumbnails:
No thumbnails
With thumbnails






.... much better with thumbnails, isn't it?