How to display image on Windows with Go?

What is the easiest way for Go to display an image on Windows? I have this tutorial based snippet:

package main import ( "image" "image/color" "image/draw" ) func main() { m := image.NewRGBA(image.Rect(0, 0, 640, 480)) blue := color.RGBA{0, 0, 255, 255} draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src) } 

But how do I display the m object? I would like to open a window and display the image there, and not write it to a file first.

+5
source share
1 answer

The package has a sample image view gxui , it displays an image selected from fragments of the command line. The result can be seen here . This is probably one of the easiest approaches to presenting this gui using go.

Beware that gxui is experimental, and future updates may break your code.

For your request, the code will be as follows. This gives a window with an image full of blue image.

 package main import ( "image" "image/color" "image/draw" "github.com/google/gxui" "github.com/google/gxui/drivers/gl" "github.com/google/gxui/themes/dark" ) func appMain(driver gxui.Driver) { width, height := 640, 480 m := image.NewRGBA(image.Rect(0, 0, width, height)) blue := color.RGBA{0, 0, 255, 255} draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src) // The themes create the content. Currently only a dark theme is offered for GUI elements. theme := dark.CreateTheme(driver) img := theme.CreateImage() window := theme.CreateWindow(width, height, "Image viewer") texture := driver.CreateTexture(m, 1.0) img.SetTexture(texture) window.AddChild(img) window.OnClose(driver.Terminate) } func main() { gl.StartDriver(appMain) } 

I confirm that this works on Windows and Linux. This probably works on MacOSX.

You might want to explore a more stable package if it is designed for production. For example, go-qml or go-qt5, as mentioned in ComputerFellow

+6
source

All Articles