Display Images File Handling Fyne GUI Golang tutorial 47

Display Images File Handling Fyne GUI Golang tutorial 47

This tutorial is for you if you Want to create an image Viewer app / software in Golang & Fyne.

You can open .jpg & .png files easily.

Dialog Box to open Files

dialog.NewFileOpen()

Input Output utility to ReadAll

data, _ := ioutil.ReadAll(uc)

Static Rosource take 2 arguments i.e name & content.
content is data we will receive from ioutil.ReadAll()

res := fyne.NewStaticResource(uc.URI().Name(), data)

Image widget to display image from static resource

img := canvas.NewImageFromResource(res)

Source Code

<div>
<div>package main</div>
<div>// import fyne</div>
<div>import (</div>
<div>    "io/ioutil"</div>
<div>    "fyne.io/fyne/v2"</div>
<div>    "fyne.io/fyne/v2/app"</div>
<div>    "fyne.io/fyne/v2/canvas"</div>
<div>    "fyne.io/fyne/v2/dialog"</div>
<div>    "fyne.io/fyne/v2/storage"</div>
<div>    "fyne.io/fyne/v2/widget"</div>
<div>)</div>
<div>func main() {</div>
<div>    // New app</div>
<div>    a := app.New()</div>
<div>    // New title</div>
<div>    w := a.NewWindow("file Handling - Open Images")</div>
<div>    //resize</div>
<div>    w.Resize(fyne.NewSize(400, 400))</div>
<div>    btn := widget.NewButton("Open .jpg &amp; .Png", func() {</div>
<div>        // dialog for opening files</div>
<div>        // 2 arguments</div>
<div>        fileDialog := dialog.NewFileOpen(</div>
<div>            // _ to ignore error</div>
<div>            func(uc fyne.URIReadCloser, _ error) {</div>
<div>                // reader to read data</div>
<div>                data, _ := ioutil.ReadAll(uc)</div>
<div>                // static resource</div>
<div>                // 2 arguments</div>
<div>                // first is file name (string)</div>
<div>                // second is data from reader</div>
<div>                res := fyne.NewStaticResource(uc.URI().Name(), data)</div>
<div>                // Now image widget to display our image</div>
<div>                img := canvas.NewImageFromResource(res)</div>
<div>                // setup new window for image and set content</div>
<div>                w := fyne.CurrentApp().NewWindow(uc.URI().Name())</div>
<div>                w.SetContent(img)</div>
<div>                // resize window</div>
<div>                w.Resize(fyne.NewSize(400, 400))</div>
<div>                w.Show() // display our image</div>
<div>            }, w)</div>
<div>        // filtering files</div>
<div>        fileDialog.SetFilter(</div>
<div>            // filter jpg and png</div>
<div>            // ignore rest of the files</div>
<div>            storage.NewExtensionFileFilter([]string{".png", ".jpg"}))</div>
<div>        fileDialog.Show()</div>
<div>        // we are done :)</div>
<div>    })</div>
<div>    // display button in parent window</div>
<div>    w.SetContent(btn)</div>
<div>    w.ShowAndRun()</div>
<div>}</div>
</div>

 

Download Code

Fyne Golang GUI Course

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: