如何使用Go在Windows上显示图像?
Go程序在Windows上显示图像的最简单方法是什么?我有一个基于教程的片段:如何使用Go在Windows上显示图像?,windows,image,go,Windows,Image,Go,Go程序在Windows上显示图像的最简单方法是什么?我有一个基于教程的片段: 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(), &
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)
}
但是如何显示对象m?我想弹出一个窗口并在那里显示图像,而不是先将其写入文件。包中有一个图像查看器示例,它显示从命令行框架中选择的图像。结果可见一斑。这可能是使用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)
// 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)
}
我确认这在Windows和Linux上有效。它可能在MacOSX上工作
如果这是用于生产的,您可能需要寻找一个更稳定的包。例如,ComputerFellow提到的go-qml或go-qt5,经过大量的谷歌搜索后,这似乎是唯一简单的方法。现在gl库不再维护,所以不要再使用此代码