Ottenere un'immagine del Form

Da Gambas-it.org - Wikipedia.

Per ottenere un'immagine del Form, comprensivo di ogni Oggetto grafico figlio in esso contenuto, è possibile adottare almeno tre modalità.


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à una Picture dell'Oggetto grafico (il Form in tal caso) posto a una distanza x, y del medesimo dall'angolo alto sinistro dello schermo.

Nell'esempio pratico che segue, porremo sul Form un TextBox, una PictureBox, per far mostrare un qualsiasi file immagine, e un Button. Si provvederà a effettuare la schermata del Form, pochissimo tempo dopo che questo si sarà attivato.

Public Sub Form_Open()
 
 TextBox1.Text = "Gambas"
 PictureBox1.Picture = Picture.Load("/percorso/del/file/immagine.png")
 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"
  
End 

Public Sub Form_Activate()
 
 Dim pc As Picture
  
' Resta in attesa un po' per consentire che il Form si apra completamente mostrando anche gli Oggetti grafici "figli" da esso contenuti:
 Wait 0.5
  
' Effettua la schermata del Form, generando un Oggetto di tipo "Picture":
 pc = Desktop.Screenshot(Me.ScreenX, Me.ScreenY, Me.W, Me.H)
  
' Salva la schermata in un file immagine di tipo "png":
 pc.Save("/tmp/test.png", 100)
  
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 del medesimo dall'angolo alto sinistro dello schermo.

Nell'esempio pratico che segue, porremo sul Form un TextBox, una PictureBox, per far mostrare un qualsiasi file immagine, e un Button. Anche in questo caso si provvederà a effettuare la schermata del Form, pochissimo tempo dopo che questo si sarà attivato.

Public Sub Form_Open()
 
 TextBox1.Text = "Gambas"
 PictureBox1.Picture = Picture.Load("/percorso/del/file/immagine.png")
 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"
  
End
 
Public Sub Form_Activate()
 
 Dim dw As DesktopWindow
 Dim pc As Picture
  
' Resta in attesa un po' per consentire che il Form si apra completamente mostrando anche gli Oggetti "figli" da esso contenuti:
 Wait 0.5
  
' Genera una variabile di tipo "DesktopWindow" che punta al Form, avendo individuato il numero identificativo della finestra del Form medesimo:
 With dw = New DesktopWindow(Me.Id)
' Effettua la schermata del Form, 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)
  
End


Uso delle risorse del Componente gb.media

Per catturare l'immagine del Form, si potranno usare anche le risorse del Componente gb.media.

Mostriamo un esempio pratico:

 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
 Dim appoggio As String

 appoggio = Temp

 pl = New MediaPipeline 
 
 src = New MediaControl(pl, "ximagesrc")
 src["xid"] = Me.Id
 vdc = New MediaControl(pl, "videoconvert")
 enc = New MediaControl(pl, "pngenc")
 snk = New MediaControl(pl, "filesink")
 snk["location"] = appoggio

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

 pl.Play()

 Repeat 
   Wait 0.01
 Until Right(File.Load(appoggio), SizeOf(gb.Integer)) = MkInt(&826042AE)

' L'immagine della GridView è salvata in un file di formato PNG:
 Copy appoggio To "/tmp/immagine.png"

 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 TextBox, una PictureBox, per far mostrare un qualsiasi file immagine, e un Button. Anche in questo caso si provvederà a effettuare la schermata del Form, pochissimo tempo dopo che questo si sarà attivato.

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()

 TextBox1.Text = "Gambas"
 PictureBox1.Picture = Picture.Load("' /percorso/del/file/immagine.png")
 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"
 
End

Public Sub Form_Activate()
 
 Dim dsp As Pointer
 Dim xi As XImage
 Dim im As Image
 Dim st As Stream
 Dim i As Integer
  
' Resta in attesa un po' per consentire che il Form si apra completamente mostrando anche gli Oggetti "figli" da esso contenuti:
 Wait 0.5
  
 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, Me.Handle, 0, 0, Me.W, Me.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(Me.W, Me.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:
 im.Save("/percorso/del/file/immagine.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()

 TextBox1.Text = "Gambas"
 PictureBox1.Picture = Picture.Load("' /percorso/del/file/immagine.png")
 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"
 
End

Public Sub Form_Activate()
 
 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
  
' Resta in attesa un po' per consentire che il Form si apra completamente mostrando anche gli Oggetti "figli" da esso contenuti:
 Wait 0.5
  
 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, Me.Handle, 0, 0, Me.W, Me.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 (Me.W * Me.H * 4) - 1
   bb.Push(Byte@(xi.data + i))
 Next
 
' Creiamo un semplice oggetto di tipo "Image":
 im = New Image(Me.W, Me.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:
 im.Save("/percorso/del/file/immagine.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.4.7 "

Mostriamo di seguito un semplice esempio analogo a quelli precedentemente visti:

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.4.7"

' 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"
 PictureBox1.Picture = Picture.Load("' /percorso/del/file/immagine.png")
 Button1.Foreground = Color.Red
 Button1.Text = "Gambas"
 
End 

Public Sub Form_Activate()
 
 Dim dsp, immago As Pointer
 Dim xi As XImage
  
' Resta in attesa un po' per consentire che il Form si apra completamente mostrando anche gli Oggetti "figli" da esso contenuti:
 Wait 0.5
  
 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, Me.Handle, 0, 0, Me.W, Me.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(Me.W, Me.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("/percorso/dove/salvare/il/file/immagine.xxx")
  
' Va in chiusura:
 imlib_free_image()
 XCloseDisplay(dsp)

End