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