Differenze tra le versioni di "Scaricare una immagine da un sito internet e mostrarla direttamente in una PictureBox mediante le funzioni esterne del API di GTK+3"

Da Gambas-it.org - Wikipedia.
 
(Una versione intermedia di uno stesso utente non è mostrata)
Riga 1: Riga 1:
Per scaricare una immagine da un sito internet e farla mostrare direttamente in una ''PictureBox'', è possibile utilizzare alcune funzioni esterne dell'API di GTK+3.
+
#REDIRECT [[Scaricare_una_immagine_da_un_sito_internet#Usando_le_funzioni_esterne_della_libreria_libgdk_pixbuf-2.0]]
 
 
Va sottolineato che la seguente pagina si riferisce esclusivamente ai file immagine di formato <FONT Color=#B22222>.png</font> .
 
 
 
E' necessario avere installata nel sistema e richiamare in Gambas la libreria dinamica condivisa: "''libgtk-3.so.0.1800.9''".
 
 
 
 
 
Mostriamo un esempio pratico in ambiente grafico. E' necessario attivare anche i Componenti ''gb.net'' e ''gb.net.curl''.
 
Private http As New HttpClient As "http"
 
Private loader As Pointer
 
 
 
Library "libgtk-3:0.1800.9"
 
 
<FONT Color=gray>' ''GdkPixbufLoader * gdk_pixbuf_loader_new(void)''
 
' ''Creates a new pixbuf loader object.''</font>
 
Private Extern gdk_pixbuf_loader_new() As Pointer
 
 
<FONT Color=gray>' ''gboolean gdk_pixbuf_loader_write (GdkPixbufLoader *loader, const guchar *buf, gsize count, GError **error)''
 
' ''Cause a pixbuf loader to parse the next count bytes of an image.''</font>
 
Private Extern gdk_pixbuf_loader_write(lo As Pointer, buf As Pointer, i As Integer, po As Pointer)
 
 
<FONT Color=gray>' ''gboolean gdk_pixbuf_loader_close (GdkPixbufLoader *loader, GError **error)''
 
' ''Informs a pixbuf loader that no further writes.''</font>
 
Private Extern gdk_pixbuf_loader_close(lo As Pointer, po As Pointer)
 
 
<FONT Color=gray>' ''GdkPixbuf * gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader)''
 
' ''Queries the GdkPixbuf that a pixbuf loader is currently creating.''</font>
 
Private Extern gdk_pixbuf_loader_get_pixbuf(lo As Pointer) As Pointer
 
 
<FONT Color=gray>' ''guchar * gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf)''
 
' ''Queries a pointer to the pixel data of a pixbuf.''</font>
 
Private Extern gdk_pixbuf_get_pixels(GdkPixbuf As Pointer) As Pointer
 
 
<FONT Color=gray>' ''int gdk_pixbuf_get_width (const GdkPixbuf *pixbuf)''
 
' ''Queries the width of a pixbuf.''</font>
 
Private Extern gdk_pixbuf_get_width(GdkPixbuf As Pointer) As Integer
 
 
<FONT Color=gray>' ''int gdk_pixbuf_get_height (const GdkPixbuf *pixbuf)''
 
' ''Queries the height of a pixbuf.''</font>
 
Private Extern gdk_pixbuf_get_height(GdkPixbuf As Pointer) As Integer
 
 
<FONT Color=gray>' ''int gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf)''
 
' ''Queries the number of channels of a pixbuf.''</font>
 
Private Extern gdk_pixbuf_get_n_channels(GdkPixbuf As Pointer) As Integer
 
 
<FONT Color=gray>' ''gboolean gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf)''
 
' ''Queries whether a pixbuf has an alpha channel (opacity information).''</font>
 
Private Extern gdk_pixbuf_get_has_alpha(GdkPixbuf As Pointer) As Boolean
 
 
<FONT Color=gray>' ''void g_object_unref (gpointer object)''
 
' ''Decreases the reference count of object.''</font>
 
Private Extern g_object_unref(_object As Pointer)
 
 
 
'''Public''' Sub Button1_Click()
 
 
<FONT Color=gray>' ''Invia la richiesta al server per scaricare il file immagine:''</font>
 
  http.URL = "<FONT Color=gray>http&#058;//indirizzo/web/del/file/immagine.png''</font>"
 
  http.Timeout = 15 
 
  http.Get()
 
   
 
'''End'''
 
 
 
'''Public''' Sub http_Finished()
 
 
 
  Dim s As String
 
 
 
  http.Close 
 
 
 
  s = http.Peek()
 
  Print "Immagine scaricata !\n\nDimensione: "; Len(s); " byte"
 
 
 
  Immagine(s)
 
 
 
'''End'''
 
 
 
'''Private''' Procedure Immagine(im As String)
 
 
 
  Dim bb As Byte[]
 
  Dim w, h As Integer
 
  Dim Pixbuf, dati As Pointer
 
  Dim c As Byte
 
  Dim bo As Boolean
 
  Dim st As Stream
 
  Dim imago As Image
 
 
 
  bb = Byte[].FromString(im)
 
 
 
  loader = gdk_pixbuf_loader_new()
 
 
 
  gdk_pixbuf_loader_write(loader, bb.data, bb.count, 0)
 
 
 
  gdk_pixbuf_loader_close(loader, 0)
 
 
 
  pixbuf = gdk_pixbuf_loader_get_pixbuf(loader)
 
 
 
  w = gdk_pixbuf_get_width(Pixbuf)
 
  Print "Larghezza: "; w; " pixel"
 
  h = gdk_pixbuf_get_height(Pixbuf)
 
  Print "Altezza:  "; h; " pixel"
 
  c = gdk_pixbuf_get_n_channels(Pixbuf)
 
  Print "Canali:    "; c
 
  bo = gdk_pixbuf_get_has_alpha(Pixbuf)
 
  If bo = False Then Error.Raise("L'immagine scaricata è priva del canale Alfa !")
 
 
 
  dati = gdk_pixbuf_get_pixels(Pixbuf)
 
  If IsNull(dati) Then Error.Raise("Impossibile ottenere un 'Puntatore' ai dati dei pixel dell'immagine !")
 
 
 
<FONT Color=gray>' ''Carichiamo i dati nel vettore "bb[]" per gestire successivamente i dati grezzi dei pixel:''</font>
 
  st = Memory dati For Read
 
 
 
  bb = New Byte[w * h * c]
 
  bb.Read(st, 0, bb.count)
 
 
 
  st.Close
 
 
 
<FONT Color=gray>' ''Viene preparata la variabile di tipo immagine per la gestione dei dati del futuro nuovo file immagine:''</font>
 
  With imago = New Image(w, h, 0, 0)
 
    st = Memory .Data For Write
 
  End With
 
 
 
<FONT Color=gray>' ''Scrive i dati presenti nel vettore "bb[]" nella varibile di tipo "Image":''</font>
 
  bb.Write(st, 0, bb.Count)
 
 
 
  st.Close
 
 
 
  With PictureBox1
 
    .W = w
 
    .H = h
 
    .Picture = imago.Picture
 
  End With
 
 
 
'''End'''
 
 
 
'''Public''' Sub Form_Close()
 
 
 
  Chiude()
 
 
 
'''End'''
 
 
 
'''Private''' Procedure Chiude()
 
 
 
  g_object_unref(loader)
 
  Me.Close
 
 
 
'''End'''
 

Versione attuale delle 19:27, 3 gen 2022