如何在Windows上使用Go显示图像?

huangapple go评论98阅读模式
英文:

How to display an image on Windows with Go?

问题

在Go程序中,在Windows上显示图像的最简单方法是使用第三方库。一个常用的库是github.com/lxn/walk,它提供了创建GUI应用程序的功能。以下是使用该库在Windows上显示图像的示例代码:

package main

import (
	"image"
	"image/color"
	"image/draw"

	"github.com/lxn/walk"
)

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)

	// 创建一个窗口
	mainWindow, _ := walk.NewMainWindow()

	// 创建一个自定义的控件用于显示图像
	imageView, _ := walk.NewImageView(mainWindow)
	imageView.SetImage(m)

	// 将图像控件添加到窗口中
	mainWindow.SetLayout(walk.NewVBoxLayout())
	mainWindow.SetTitle("Image Display")
	mainWindow.SetSize(image.Size())
	mainWindow.SetVisible(true)

	// 运行应用程序的消息循环
	walk.Run()
}

这段代码使用github.com/lxn/walk库创建了一个窗口,并在窗口中显示了图像。你可以根据需要调整窗口的大小和标题。运行程序后,会弹出一个窗口显示图像。

英文:

What's the easiest way for a Go program to display an image on Windows? I have this fragment based on a tutorial:

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 to I display the object m? I'd like to pop up a window and display the image there, not write it out to a file first.

答案1

得分: 12

gxui 包中有一个图像查看器示例,它显示从命令行片段中选择的图像。结果可以在 这里 看到。这可能是使用 Go 来呈现此 GUI 的较简单的方法之一。

请注意,gxui 是实验性的,未来的更新可能会破坏你的代码。

根据你的要求,以下是代码。它会生成一个显示你的图像(一个全蓝图像)的窗口。

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)

	// 主题创建内容。目前只提供了一个用于 GUI 元素的暗主题。
	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)
}

我确认这在 Windows 和 Linux 上可以工作。它可能也适用于 MacOSX。

如果这是用于生产环境,你可能希望考虑更稳定的包,例如 go-qml 或 go-qt5,正如 ComputerFellow 提到的那样。

英文:

There is an image viewer sample in the package gxui, it shows an image selected from command line frags. The result can be seen here. It is likely one of the simpler approaches to present this gui using go.

Beware that gxui is experiental and future updates might break your code.

For your request, the code would be the following. It yields a window showing your image, a full 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 this works on Windows and Linux. It likely works on MacOSX.

You might want to look into a more stable package if this is for production. For example go-qml or go-qt5 as mentioned by ComputerFellow

答案2

得分: 3

更新后的使用Fyne的代码如下,假设有一个generateImage函数用于创建要渲染的image.Image

package main

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

func main() {
	a := app.New()
	w := a.NewWindow("Images")

	img := canvas.NewImageFromImage(generateImage())
	w.SetContent(img)
	w.Resize(fyne.NewSize(640, 480))

	w.ShowAndRun()
}

这段代码使用Fyne库创建了一个窗口,并在窗口中显示了通过generateImage函数生成的图像。窗口的大小被设置为640x480像素。

英文:

Updated answer using Fyne would be as follows, assuming a generateImage function that creates the image.Image to be rendered.

package main

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

func main() {
	a := app.New()
	w := a.NewWindow("Images")

	img := canvas.NewImageFromImage(generateImage())
	w.SetContent(img)
	w.Resize(fyne.NewSize(640, 480))

	w.ShowAndRun()
}

huangapple
  • 本文由 发表于 2015年4月12日 11:46:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/29585727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定