Differenze tra le versioni di "Ottenere una schermata (screenshot) del desktop con le risorse del API di X11 e di Cairo"

Da Gambas-it.org - Wikipedia.
Riga 1: Riga 1:
 
E' possibile ottenere una schermata del desktop utilizzando alcune risorse esterne del API di X11 e di Cairo.
 
E' possibile ottenere una schermata del desktop utilizzando alcune risorse esterne del API di X11 e di Cairo.
  
Per poter fruire di tali risorse, è necessario richiamare in Gambas le librerie dinamiche e condivise:
+
Per poter fruire di tali risorse, è necessario richiamare in Gambas le librerie condivise:
 
* libX11.so.6.3.0
 
* libX11.so.6.3.0
* libcairo.so.2.11301.0
+
* libcairo.so.2.11600.0
  
  
Riga 38: Riga 38:
 
   
 
   
 
   
 
   
  Library "libcairo:2.11301.0"
+
  Library "libcairo:2.11600.0"
 
   
 
   
 
  <FONT Color=gray>' ''cairo_surface_t * cairo_xlib_surface_create (Display *dpy, Drawable drawable, Visual *visual, int width, int height)''
 
  <FONT Color=gray>' ''cairo_surface_t * cairo_xlib_surface_create (Display *dpy, Drawable drawable, Visual *visual, int width, int height)''
Riga 60: Riga 60:
 
    
 
    
 
  <FONT Color=gray>' ''Prova a connettersi con il display:''</font>
 
  <FONT Color=gray>' ''Prova a connettersi con il display:''</font>
    dsp = XOpenDisplay(Null)
+
  dsp = XOpenDisplay(Null)
    If IsNull(dsp) Then Error.Raise("Impossibile aprire una connessione al server X !")
+
  If dsp == 0 Then Error.Raise("Impossibile aprire una connessione al server X !")
 
      
 
      
    scr = XDefaultScreen(dsp)
+
  scr = XDefaultScreen(dsp)
 +
  root = XDefaultRootWindow(dsp)
 
      
 
      
    root = XDefaultRootWindow(dsp)
+
  vis = XDefaultVisual(dsp, scr)
   
+
  If vis == 0 Then Error.Raise("Errore !")
    vis = XDefaultVisual(dsp, scr)
+
 
   
+
  wi = XDisplayWidth(dsp, scr)
    wi = XDisplayWidth(dsp, scr)
+
  he = XDisplayHeight(dsp, scr)
   
 
    he = XDisplayHeight(dsp, scr)
 
 
      
 
      
 
  <FONT Color=gray>' ''Ottiene la superficie di root del display:''</font>
 
  <FONT Color=gray>' ''Ottiene la superficie di root del display:''</font>
    surface = cairo_xlib_surface_create(dsp, root, vis, wi, he)
+
  surface = cairo_xlib_surface_create(dsp, root, vis, wi, he)
   
+
  If surface == 0 Then Error.Raise("Errore !")
    err = cairo_surface_write_to_png(surface, "<FONT Color=gray>''/tmp/schermata.png''</font>")
+
 
    If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !")
+
  err = cairo_surface_write_to_png(surface, "<FONT Color=gray>''/tmp/schermata.png''</font>")
 +
  If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !")
 
      
 
      
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
 
  <FONT Color=gray>' ''Va in chiusura:''</font>
    cairo_surface_destroy(surface)
+
  cairo_surface_destroy(surface)
    XCloseDisplay(dsp)
+
  XCloseDisplay(dsp)
 
    
 
    
 
  '''End'''
 
  '''End'''
 
  
  

Versione delle 19:09, 1 dic 2021

E' possibile ottenere una schermata del desktop utilizzando alcune risorse esterne del API di X11 e di Cairo.

Per poter fruire di tali risorse, è necessario richiamare in Gambas le librerie condivise:

  • libX11.so.6.3.0
  • libcairo.so.2.11600.0


Mostriamo un esempio, nel quale verrà effettuata una schermata dell'intero desktop. In fine la schermata verrà salvata in un file di formato .png:

Library "libX11:6.3.0"

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

' int XDefaultScreen(display)
' Returns the default screen number.
Private Extern XDefaultScreen(display As Pointer) As Integer

' Window XDefaultRootWindow(display)
' Returns the root window for the default screen.
Private Extern XDefaultRootWindow(display As Pointer) As Long

' Visual *XDefaultVisual(display, screen_number)
' Returns the default visual type for the specified screen.
Private Extern XDefaultVisual(display As Pointer, scr_num As Integer) As Pointer

' int XDisplayWidth(display, screen_number)
' Returns the width of the screen in pixels.
Private Extern XDisplayWidth(display As Pointer, scr_num As Integer) As Integer

' int XDisplayHeight(display, screen_number)
' Returns an integer that describes the height of the screen in pixels.
Private Extern XDisplayHeight(display As Pointer, scr_num As Integer) As Integer

' 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 "libcairo:2.11600.0"

' cairo_surface_t * cairo_xlib_surface_create (Display *dpy, Drawable drawable, Visual *visual, int width, int height)
' Creates an Xlib surface that draws to the given drawable.
Private Extern cairo_xlib_surface_create(dpy As Pointer, drawable As Long, visual As Pointer, width As Integer, height As Integer) As Pointer

' cairo_status_t cairo_surface_write_to_png(cairo_surface_t *surface, const char *filename)
' Writes the contents of surface to a new file filename as a PNG image.
Private Extern cairo_surface_write_to_png(cairo_surface As Pointer, filename As String) As Integer

' void cairo_surface_destroy(cairo_surface_t *surface)
' Decreases the reference count on surface by one.
Private Extern cairo_surface_destroy(cairo_surface As Pointer)


Public Sub Main()
 
 Dim dsp, vis, surface As Pointer
 Dim root As Long
 Dim scr, wi, he, err As Integer
 
' Prova a connettersi con il display:
 dsp = XOpenDisplay(Null)
 If dsp == 0 Then Error.Raise("Impossibile aprire una connessione al server X !")
   
 scr = XDefaultScreen(dsp)
 root = XDefaultRootWindow(dsp)
   
 vis = XDefaultVisual(dsp, scr)
 If vis == 0 Then Error.Raise("Errore !")
 
 wi = XDisplayWidth(dsp, scr)  
 he = XDisplayHeight(dsp, scr)
   
' Ottiene la superficie di root del display:
 surface = cairo_xlib_surface_create(dsp, root, vis, wi, he)
 If surface == 0 Then Error.Raise("Errore !")
  
 err = cairo_surface_write_to_png(surface, "/tmp/schermata.png")
 If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !")
   
' Va in chiusura:
 cairo_surface_destroy(surface)
 XCloseDisplay(dsp)
 
End


Riferimenti