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.
Versione del 28 lug 2017 alle 04:18 di Vuott (Discussione | contributi)
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.
Va sottolineato che la seguente pagina si riferisce ai file immagine di formato .png .
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" ' GdkPixbufLoader * gdk_pixbuf_loader_new(void) ' Creates a new pixbuf loader object. Private Extern gdk_pixbuf_loader_new() As Pointer ' 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. Private Extern gdk_pixbuf_loader_write(lo As Pointer, buf As Pointer, i As Integer, po As Pointer) ' gboolean gdk_pixbuf_loader_close (GdkPixbufLoader *loader, GError **error) ' Informs a pixbuf loader that no further writes. Private Extern gdk_pixbuf_loader_close(lo As Pointer, po As Pointer) ' GdkPixbuf * gdk_pixbuf_loader_get_pixbuf (GdkPixbufLoader *loader) ' Queries the GdkPixbuf that a pixbuf loader is currently creating. Private Extern gdk_pixbuf_loader_get_pixbuf(lo As Pointer) As Pointer ' guchar * gdk_pixbuf_get_pixels (const GdkPixbuf *pixbuf) ' Queries a pointer to the pixel data of a pixbuf. Private Extern gdk_pixbuf_get_pixels(GdkPixbuf As Pointer) As Pointer ' int gdk_pixbuf_get_width (const GdkPixbuf *pixbuf) ' Queries the width of a pixbuf. Private Extern gdk_pixbuf_get_width(GdkPixbuf As Pointer) As Integer ' int gdk_pixbuf_get_height (const GdkPixbuf *pixbuf) ' Queries the height of a pixbuf. Private Extern gdk_pixbuf_get_height(GdkPixbuf As Pointer) As Integer ' int gdk_pixbuf_get_n_channels (const GdkPixbuf *pixbuf) ' Queries the number of channels of a pixbuf. Private Extern gdk_pixbuf_get_n_channels(GdkPixbuf As Pointer) As Integer ' gboolean gdk_pixbuf_get_has_alpha (const GdkPixbuf *pixbuf) ' Queries whether a pixbuf has an alpha channel (opacity information). Private Extern gdk_pixbuf_get_has_alpha(GdkPixbuf As Pointer) As Boolean ' void g_object_unref (gpointer object) ' Decreases the reference count of object. Private Extern g_object_unref(_object As Pointer) Public Sub Button1_Click() ' Invia la richiesta al server per scaricare il file immagine: http.URL = "http://indirizzo/web/del/file/immagine.png" 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 !") ' Carichiamo i dati nel vettore "bb[]" per gestire successivamente i dati grezzi dei pixel: st = Memory dati For Read bb = New Byte[w * h * c] bb.Read(st, 0, bb.count) st.Close ' Viene preparata la variabile di tipo immagine per la gestione dei dati del futuro nuovo file immagine: With imago = New Image(w, h, 0, 0) st = Memory .Data For Write End With ' Scrive i dati presenti nel vettore "bb[]" nella varibile di tipo "Image": 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