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.
(Creata pagina con "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 sott...") |
|||
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. | 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, poiché | + | Va sottolineato che, poiché il componente ''gb.image'' ha il canale Alpha "invertito" (0xFF = trasparente, 0 = opaco) rispetto al formato standard RGBA <SUP>[ [[#Note|nota 1]] ]</sup>, va effettuata la necessaria correzione con apposita sotto-procedura. |
Riga 156: | Riga 156: | ||
'''End''' | '''End''' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | =Note= | ||
+ | [1] Vedi la documentazione Gambas al riguardo: http://gambaswiki.org/wiki/doc/imageconv |
Versione delle 19:42, 10 nov 2016
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, poiché il componente gb.image ha il canale Alpha "invertito" (0xFF = trasparente, 0 = opaco) rispetto al formato standard RGBA [ nota 1 ], va effettuata la necessaria correzione con apposita sotto-procedura.
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" Library "libgtk-3:0.1000.8" ' 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 Public Sub Button1_Click() ' Invia_richiesta al server per scaricare il file immagine: http.URL = "https://lh3.googleusercontent.com/_Oijhf1ZPv-4/TVzeP4NPMDI/AAAAAAAAB10/FhFzcvQLXLw/s800/colorwheel.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, bb2 As Byte[] Dim w, h As Integer Dim loader, Pixbuf, dati As Pointer Dim c As Byte Dim bo As Boolean Dim st As Stream Dim immago 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].Read(st, 0, .count) ' Effettua la correzione della disposizione dei dati RGB dei pixel dell'immagine scaricata: bb2 = Corregge(bb) st.Close ' Viene preparata la variabile di tipo immagine per la gestione dei dati del futuro nuovo file immagine: With immago = 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": bb2.Write(st, 0, bb2.Count) st.Close With PictureBox1 .W = w .H = h .Picture = immago.Picture End With End Private Function Corregge(vett As Byte[]) As Byte[] Dim i As Integer Dim bb As Byte[] bb = New Byte[vett.Count] For i = 0 To vett.Max Step 4 bb[i + 1] = vett[i + 1] bb[i + 2] = vett[i] bb[i] = vett[i + 2] bb[i + 3] = vett[i + 3] Next Return bb End Public Sub Button2_Click() Me.Close End
Note
[1] Vedi la documentazione Gambas al riguardo: http://gambaswiki.org/wiki/doc/imageconv