duanlinghe8417
2014-03-30 17:07
浏览 113
已采纳

在Golang draw2d包中填充一个像素

Is there a way to draw an image by filling one pixel at a time in Golang, preferably using the draw2d package?

For example, one can draw a line by using the stroke() command as such (from their getting started page):

 package main

impenter code hereort (
        "bufio"
        "fmt"
        "log"
        "os"

        "code.google.com/p/draw2d/draw2d"
        "image"
        "image/png"
)

func saveToPngFile(filePath string, m image.Image) {
        f, err := os.Create(filePath)
        if err != nil {
                log.Println(err)
                os.Exit(1)
        }
        defer f.Close()
        b := bufio.NewWriter(f)
        err = png.Encode(b, m)
        if err != nil {
                log.Println(err)
                os.Exit(1)
        }
        err = b.Flush()
        if err != nil {
                log.Println(err)
                os.Exit(1)
        }
        fmt.Printf("Wrote %s OK.
", filePath)
}

func main() {
        i := image.NewRGBA(image.Rect(0, 0, 200, 200))
        gc := draw2d.NewGraphicContext(i)
        gc.MoveTo(10.0, 10.0)
        gc.LineTo(100.0, 10.0)
        gc.Stroke()
        saveToPngFile("TestPath.png", i)
}

But how could I fill just one pixel, instead of connecting the 2 points? Draw2d is not a necessity, it is simply what I thought would be easiest.

图片转代码服务由CSDN问答提供 功能建议

是否可以通过在Golang中一次填充一个像素来绘制图像,最好使用draw2d包?

例如,可以这样使用stroke()命令来画一条线(从其入门页面开始):

  打包主文件
 
输入此处的字符代码(
“ bufio” 
“ fmt” 
“ log” 
“ os” 
 
“ code.google.com/p/draw2d/draw2d"
”图像 “ 
” image / png“ 
)
 
func saveToPngFile(filePath string,m image.Image){
f,err:= os.Create(filePath)
 if err!= nil {
日志。  Println(err)
 os.Exit(1)
} 
推迟f.Close()
b:= bufio.NewWriter(f)
 err = png.Encode(b,m)
 if err!  = nil {
 log.Println(err)
 os.Exit(1)
} 
 err = b.Flush()
 if err!= nil {
 log.Println(err)
 os  .Exit(1)
} 
 fmt。  Printf(“写%s OK。
”,filePath)
} 
 
func main(){
i:= image.NewRGBA(image.Rect(0,0,200,200))
 gc:  = draw2d.NewGraphicContext(i)
 gc.MoveTo(10.0,10.0)
 gc.LineTo(100.0,10.0)
 gc.Stroke()
 saveToPngFile(“ TestPath.png”,i)
} \  n   
 
 

但是我怎么能只填充一个像素而不是连接两个点呢? Draw2d不是必需的,只是我认为最简单的。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douqian5553 2014-03-30 17:26
    已采纳

    In a vector graphics library like draw2d or Cairo, there is rarely pixel addressing because the model is not a raster of bits. Instead, the vector graphics model has you do things like draw lines in an abstract Euclidean space. This allows device independent drawing and prevents pixel addressing because there are no pixels.

    However, draw2d has the standard Package image underlying it, which does allow pixel addressing as with the function Set.

    If you are doing many pixels, expect it to be slow. And maybe learn about raster operations.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题