Creare un file immagine dagli oggetti presenti in un Contenitore

Da Gambas-it.org - Wikipedia.

Il caso è quello in cui si intende creare un file immagine da un Contenitore qualsiasi, nel quale siano presenti uno o più Oggetti grafici, magari anche colorati e contenenti del testo.
L'immagine salvata nel file dovrà rappresentare i Controlli presenti nel Contenitore, tenendo conto delle loro proprietà (colore, dimensioni, eventuale testo, etc.) specificate al momento della creazione dagli Oggetti grafici.

Usando la Classe Paint

Nel seguente esempio pratico si hanno sul Form (Contenitore) due Label e un Button. Si intende ottenere un file immagine di quanto appare nel Form, escludendo il Button.

Private lb1 As Label
Private lb2 As Label
Private Button1 As Button
 

Public Sub _new()
 
 With Me
   .Center
   .W = 700
   .H = 600
 End With
 With lb1 = New Label(Me)
   .X = 100
   .Y = 100
   .W = 100
   .H = 50
   .Background = Color.Red
   .Foreground = Color.Yellow
   .Alignment = Align.Center
   .Text = "Label rossa"
 End With
 With lb2 = New Label(Me)
   .X = 200
   .Y = 200
   .W = 100
   .H = 50
   .Background = Color.Yellow
   .Foreground = Color.Red
   .Alignment = Align.Right
   .Text = "Label gialla"
 End With
 With Button1 = New Button(Me) As "Button1"
   .W = 60
   .H = 60
   .X = Me.W - (Button1.W * 2)
   .Y = Me.H - (Button1.H * 2)
 End With
 
End


Public Sub Button1_Click()
 
 Dim ob As Object
 Dim im As Image
 
' Si crea la superficie dell'immagine che dovrà contenere la riproduzione grafica di quanto presente sul Form:
 im = New Image(600, 400, Color.White, Image.Standard)

 With Paint
   .Begin(im)
   For Each ob In Me.Children
     If Object.Type(ob) = "Label" Then
' Il colore, con il quale saranno stampati i due oggetti "Label", lo ricaviamo dal colore di sfondo di ciascuna "Label" medesima:
       .Brush = .Color(ob.Background)
' Sarà stampato il contenuto presente nel rettangolo di coordinate e dimensioni uguali a quelle dell'oggetto da stampare:
       .Rectangle(ob.X, ob.Y, ob.W, ob.H)
       .Fill
' Ora viene impostato il colore del testo:
       .Brush = .Color(ob.Foreground)
' Viene impostato il testo con le caratteristiche originarie della "Label" in questione:
       .Text(ob.Text, ob.X, ob.Y, ob.W, ob.H, ob.Alignment)
       .Fill
     Endif
   Next
   .End
 End With
 
 im.Save("/tmp/immagine.png", 100)
   
End


Uso del Metodo ".Screenshot()" della Classe Desktop

La prima modalità prevede l'uso del Metodo ".Screenshot()" della Classe Desktop. Pertanto è necessario attivare anche i Componenti gb.desktop e gb.desktop.x11 di Gambas.

Con il Metodo ".Screenshot()" si otterrà un Picture di un Controllo grafico posto a una distanza x, y dall'angolo alto sinistro dello schermo.

Nell'esempio pratico che segue, porremo sul Form un Button, del quale si otterrà una schermata e un file immagine fonale:

Public Sub Form_Open()

 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"

End 


Public Sub Button1_Click()
 
 Dim pc As Picture
  
' Effettua la schermata del "Button", generando un Oggetto di tipo "Picture":
 pc = Desktop.Screenshot(Button1.ScreenX, Button1.ScreenY, Button1.W, Button1.H)
  
' Salva la schermata in un file immagine di tipo "png":
 pc.Save("/tmp/test.png", 100)

 Print "Immagine acquisita !"

End


Uso del Metodo ".GetScreenshot()" della Classe DesktopWindow

La seconda malità prevede l'uso del Metodo ".GetScreenshot()" della Classe DesktopWindow. Pertanto è necessario attivare anche i Componenti gb.desktop e gb.desktop.x11 di Gambas.

Con il Metodo ".GetScreenshot()" si otterrà una Picture dell'Oggetto grafico (il Form in tal caso) posto a una distanza x, y dall'angolo alto sinistro dello schermo.

Nell'esempio pratico che segue, porremo sul Form un Button, del quale si otterrà una schermata e un file immagine fonale:

Public Sub Form_Open()

  Button1.Foreground = Color.Red
  Button1.Text = "Gambas"

End
 

Public Sub Button1_Click()
 
  Dim dw As DesktopWindow
  Dim pc As Picture

' Genera una variabile di tipo "DesktopWindow" che punta al "Button", avendo individuato il numero identificativo della sua finestra:
  With dw = New DesktopWindow(Button1.Id)
' Effettua la schermata del "Button", generando un Oggetto di tipo "Picture":
    pc = .GetScreenshot(True)
  End With

' Salva la schermata in un file immagine di tipo "png":
  pc.Save("/tmp/test.png", 100)

  Print "Immagine acquisita"

End


Usando le risorse del Componente gb.media

Otterremo il file immagine di formato PNG di una GridView presente sul Form:

Public Sub Form_Open()

  Dim r, c As Byte

  With GridView1
    .Rows.Count = 5
    .Columns.Count = 5
  End With

  For r = 0 To 4
    For c = 0 To 4
       GridView1[r, c].Text = "abcde"
    Next 
  Next

End 


Public Sub Button1_Click()

  Dim pl As MediaPipeline
  Dim src, vdc, enc, snk As MediaControl

  pl = New MediaPipeline 

  src = New MediaControl(pl, "ximagesrc")
  src["xid"] = GridView1.Id
  vdc = New MediaControl(pl, "videoconvert")
  enc = New MediaControl(pl, "pngenc")
  snk = New MediaControl(pl, "filesink")
  snk["location"] = "/tmp/immagine.png"

  src.LinkTo(vdc)
  vdc.LinkTo(enc)
  enc.LinkTo(snk)

  pl.Play()

  Repeat 
    Wait 0.01
    Print pl.State
  Until Right(File.Load("/tmp/immagine.png"), SizeOf(gb.Integer)) = MkInt(&826042AE)

  pl.Stop()
  pl.Close()
  Print "Cattura immagine effettuata"

End


Uso delle funzioni esterne del API di X11

Questa modalità prevede l'uso di alcune funzioni esterne appartenenti al API del sistema grafico X11. E' pertanto richiesta la dichiarazione in Gambas della libreria condivisa: "libX11.so.6.4.0 ".

L'assegnazione dei dati dei pixel del Form, contenuti nell'area di memoria puntata dal Puntatore restituito dalla funzione esterna XGetImage( ) della libreria di X11, direttamente all'area di memoria puntata dalla Proprietà ".Data" di un Oggetto Image preliminarmente creato.

Nell'esempio pratico che segue, porremo sul Form un Button, del quale si otterrà l'immegine finale.

Library "libX11:6.4.0"

Public Struct funcs
  create_image As Pointer
  destroy_image As Pointer
  get_pixel As Pointer
  put_pixel As Pointer
  sub_image As Pointer
  add_pixel As Pointer
End Struct

Public Struct XImage
  width As Integer
  height As Integer
  xoffset As Integer
  gformat As Integer
  data As Pointer
  byte_order As Integer
  bitmap_unit As Integer
  bitmap_bit_order As Integer
  bitmap_pad As Integer
  depth As Integer
  bytes_per_line As Integer
  bits_per_pixel As Integer
  red_mask As Long
  green_mask As Long
  blue_mask As Long
  obdata As Pointer
  func As Struct Funcs
End Struct

Private Enum XYBitmap = 0, XYPixmap, ZPixmap

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display_name As Pointer) As Pointer

' unsigned long XAllPlanes()
' Returns a value with all bits set to 1 suitable for use in a plane argument to a procedure.
Private Extern XAllPlanes() As Long

' XImage *XGetImage(Display *display, Drawable d, int x, int y, unsigned int width, unsigned int height, unsigned long plane_mask, int format)
' Returns a pointer to an XImage structure.
Private Extern XGetImage(display As Pointer, d As Long, x As Integer, y As Integer, width As Integer, height As Integer, plane_mask As Long, xformat As Integer) As XImage
 
' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)


Public Sub Form_Open()

 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"

End


Public Sub Button1_Click()
 
 Dim dsp As Pointer
 Dim xi As XImage
 Dim im As Image
 Dim st As Stream
 Dim i As Integer

 dsp = XOpenDisplay(0)
 If dsp = 0 Then Error.Raise("Impossibile aprire una connessione al server X !")

' Otteniamo un puntatore alla "Struttura" contenente i dati dell'immagine disegnata nel "Form":
 xi = XGetImage(dsp, Button1.Handle, 0, 0, Button1.W, Button1.H, XAllPlanes(), ZPixmap)
 If IsNull(Image) Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dell'immagine del Form !")

' Creiamo un semplice oggetto di tipo "Image":
 im = New Image(Button1.W, Button1.H)
 If IsNull(im) Then Error.Raise("Impossibile creare un oggetto 'Image' !")

' Utilizziamo ovviamente i "Memory Stream" per scrivere nell'area di memoria dell'oggetto "Image", destinata ai dati attinenti ai pixel, il cui indirizzo di memoria è ritornato dalla Proprietà ".Data" della variabile di tipo "Image":
 st = Memory im.Data For Write

 For i = 0 To im.W * im.H * Len(im.Format)
   Write #st, Byte@(xi.data + i) As Byte
 Next
  
 st.Close
 
' Genera in fine il file immagine.
' Al nome del file immagine da creare va indicata l'estensione del formato immagine desiderato:
 im.Save("/tmp/test.XXX", 100)
  
' Va in chiusura:
 XCloseDisplay(dsp)
  
End

oppure anche così:

Library "libX11:6.4.0"

Public Struct funcs
  create_image As Pointer
  destroy_image As Pointer
  get_pixel As Pointer
  put_pixel As Pointer
  sub_image As Pointer
  add_pixel As Pointer
End Struct

Public Struct XImage
  width As Integer
  height As Integer
  xoffset As Integer
  gformat As Integer
  data As Pointer
  byte_order As Integer
  bitmap_unit As Integer
  bitmap_bit_order As Integer
  bitmap_pad As Integer
  depth As Integer
  bytes_per_line As Integer
  bits_per_pixel As Integer
  red_mask As Long
  green_mask As Long
  blue_mask As Long
  obdata As Pointer
  func As Struct Funcs
End Struct

Private Enum XYBitmap = 0, XYPixmap, ZPixmap

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display_name As Pointer) As Pointer

' unsigned long XAllPlanes()
' Returns a value with all bits set to 1 suitable for use in a plane argument to a procedure.
Private Extern XAllPlanes() As Long

' XImage *XGetImage(Display *display, Drawable d, int x, int y, unsigned int width, unsigned int height, unsigned long plane_mask, int format)
' Returns a pointer to an XImage structure.
Private Extern XGetImage(display As Pointer, d As Long, x As Integer, y As Integer, width As Integer, height As Integer, plane_mask As Long, xformat As Integer) As XImage
 
' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)


Public Sub Form_Open()

 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"

End


Public Sub Button1_Click()

 Dim dsp, p As Pointer
 Dim xi As XImage
 Dim bb As Byte[]
 Dim i As Integer
 Dim st As Stream
 Dim im As Image

 dsp = XOpenDisplay(0)
 If dsp == 0 Then Error.Raise("Impossibile aprire una connessione al server X !")

' Otteniamo un puntatore alla "Struttura" contenente i dati dell'immagine disegnata nel "Form":
 xi = XGetImage(dsp, Button1.Handle, 0, 0, Button1.W, Button1.H, XAllPlanes(), ZPixmap)
 If IsNull(Image) Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dell'immagine del Form !")

 bb = New Byte[]
  
 For i = 0 To (Button1.W * Button1.H * 4) - 1
   bb.Push(Byte@(xi.data + i))
 Next
 
' Creiamo un semplice oggetto di tipo "Image":
 im = New Image(Button1.W, Button1.H)
 If IsNull(im) Then Error.Raise("Impossibile creare un oggetto 'Image' !")
  
 p = Pointer@(Object.Address(im) + (SizeOf(gb.Pointer) * 2))
  
' Scriviamo i dati immagine grezzi, ottenuti dalla funzione XGetImage(), nell'area di memoria puntata dal membro "*data" della Struttura "GB_IMG" contenuta nel file sorgente ".../main/lib/image/gb.image.h":
 st = Memory p For Write
 bb.Write(st, 0, bb.Count)
 st.Close
  
' Genera in fine il file immagine.
' Al nome del file immagine da creare va indicata l'estensione del formato immagine desiderato:
 im.Save("/tmp/test.XXX", 100)
  
' Va in chiusura:
 XCloseDisplay(dsp)
  
End


Uso delle funzioni esterne del API di X11 e di ImLib2

L'uso delle risorse delle librerie esterne X11 e ImLib2 richiede nel codice Gambas la dichiarazione delle seguenti librerie dinamiche condivise:

  • "libX11.so.6.4.0 "
  • "libImlib2.so.1.12.2 "

Mostriamo di seguito un semplice esempio, nel quale si otterrà il file immagine di un "TextBox" posto sul Form:

Library "libX11:6.4.0"

Public Struct funcs
  create_image As Pointer
  destroy_image As Pointer
  get_pixel As Pointer
  put_pixel As Pointer
  sub_image As Pointer
  add_pixel As Pointer
End Struct

Public Struct XImage
  width As Integer
  height As Integer
  xoffset As Integer
  gformat As Integer
  data As Pointer
  byte_order As Integer
  bitmap_unit As Integer
  bitmap_bit_order As Integer
  bitmap_pad As Integer
  depth As Integer
  bytes_per_line As Integer
  bits_per_pixel As Integer
  red_mask As Long
  green_mask As Long
  blue_mask As Long
  obdata As Pointer
  func As Struct Funcs
End Struct

Private Enum XYBitmap = 0, XYPixmap, ZPixmap

' Display *XOpenDisplay(char *display_name)
' Opens a connection to the X server that controls a display.
Private Extern XOpenDisplay(display_name As Pointer) As Pointer

' unsigned long XAllPlanes()
' Returns a value with all bits set to 1 suitable for use in a plane argument to a procedure.
Private Extern XAllPlanes() As Long

' XImage *XGetImage(Display *display, Drawable d, int x, int y, unsigned int width, unsigned int height, unsigned long plane_mask, int format)
' Returns a pointer to an XImage structure.
Private Extern XGetImage(display As Pointer, d As Long, x As Integer, y As Integer, width As Integer, height As Integer, plane_mask As Long, xformat As Integer) As XImage
 
' XCloseDisplay(Display *display)
' Closes the connection to the X server for the display specified in the Display structure and destroys all windows.
Private Extern XCloseDisplay(display As Pointer)


Library "libImlib2:1.12.2"

' Imlib_Image imlib_create_image_using_data(int width, int height, DATA32 * data)
' Creates an image from the image data - in the format of a DATA32 (32 bits) per pixel in a linear array - specified with the width and the height specified.
Private Extern imlib_create_image_using_data(width As Integer, height As Integer, data As Pointer) As Pointer

' void imlib_context_set_image(Imlib_Image image)
' Sets the current image Imlib2 will be using with its function calls.
Private Extern imlib_context_set_image(Iimage As Pointer)

' void imlib_save_image(const char *filename)
' Saves the current image in the format specified by the current image's format settings to the filename.
Private Extern imlib_save_image(filename As String)

' void imlib_free_image(void)
' Frees the image that is set as the current image in Imlib2's context.
Private Extern imlib_free_image()


Public Sub Form_Open()

 TextBox1.Text = "Gambas"

End 


Public Sub Button1_Click()
 
 Dim dsp, immago As Pointer
 Dim xi As XImage

 dsp = XOpenDisplay(0)
 If dsp == 0 Then Error.Raise("Impossibile aprire una connessione al server X !")

' Otteniamo un puntatore alla "Struttura" contenente i dati dell'immagine disegnata nel "Form":
 xi = XGetImage(dsp, TextBox1.Handle, 0, 0, TextBox1.W, TextBox1.H, XAllPlanes(), ZPixmap)
 If IsNull(Image) Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dell'immagine del Form !")

' Viene creata l'immagine dai dati grezzi passando il "Puntatore" ai dati grezzi dell'immagine:
 immago = imlib_create_image_using_data(TextBox1.W, TextBox1.H, xi.data)
 If immago == 0 Then Error.Raise("Impossibile creare l'immagine dai dati grezzi acquisiti !")

 imlib_context_set_image(immago)

' L'immagine creata viene salvata in un file immagine.
' Al nome del file immagine da creare va indicata l'estensione del formato immagine desiderato:
 imlib_save_image("/tmp/test.XXX")

' Va in chiusura:
 imlib_free_image()
 XCloseDisplay(dsp)

End