Image

A canvas.Image represents a scalable image resource in Fyne. It can be loaded from a resource (as shown in the example), from an image file or from a Go image.Image in memory.

The default image fill mode is canvas.ImageFillStretch which will cause it to fill the space specified (through Resize() or layout). Alternatively you could use canvas.ImageFillContain to ensure that the aspect ratio is maintained and the image is within the bounds. Further to this you can use canvas.ImageFillOriginal (as used in the example here) which ensures that it will also have a minimum size equal to that of the original image size.

Images can be bitmap based (like PNG and JPEG) or vector based (such as SVG). Where possible we recommend scalable images as they will continue to render well as sizes change. Be careful when using original image sizes as they may not behave exactly as expected with different user interface scales. As Fyne allows the entire user interface to scale a 25px image file may not be the same height as a 25 height fyne object.

Example Code

            
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package main

import (
	"fyne.io/fyne/app"
	"fyne.io/fyne/canvas"
	"fyne.io/fyne/theme"
)

func main() {
	myApp := app.New()
	w := myApp.NewWindow("Image")

	image := canvas.NewImageFromResource(theme.FyneLogo())
	// image := canvas.NewImageFromFile(fileName)
	// image := canvas.NewImageFromImage(src)
	image.FillMode = canvas.ImageFillOriginal
	w.SetContent(image)

	w.ShowAndRun()
}