I’m working on a Go application using the fyne
library to create a graphical user interface. When I try to display a PNG image, a grid overlay appears on top of the image. I’ve tried different methods to decode and display the image, but the grid persists. Here is a simplified version of my code:
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"image/png"
"os"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Image Viewer")
// Open the image file
file, err := os.Open("path/to/image.png") // Set path
if err != nil {
fyne.LogError("Failed to open image", err)
return
}
defer file.Close()
// Decode the image
img, err := png.Decode(file)
if err != nil {
fyne.LogError("Failed to decode image", err)
return
}
// Create a canvas image from the decoded image
imageCanvas := canvas.NewImageFromImage(img)
imageCanvas.FillMode = canvas.ImageFillContain
imageCanvas.SetMinSize(fyne.NewSize(400, 300))
// Create content container
content := container.NewVBox(
widget.NewLabel("Here is your image:"),
imageCanvas,
)
// Set the content and size
myWindow.SetContent(content)
myWindow.Resize(fyne.NewSize(800, 600))
myWindow.ShowAndRun()
}
I’ve also tried using canvas.ImageFillStretch and canvas.ImageFillOriginal for the FillMode, tried to getting image by using canvas.NewImageFromFile, but neither resolves the issue. How can I fix this grid overlay problem and correctly display the PNG image in fyne? Any help or suggestions would be greatly appreciated!
sergey4qb is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.