How to Draw a rectangle in Golang?
Below is a short program to create a PNG image of 200 X 200 size. In green variable RGBA value of green color assigned
Example
package main
import (
"image"
"image/color"
"image/draw"
"image/png"
"os"
"log"
)
func main() {
rectangle := "rectangle.png"
rectImage := image.NewRGBA(image.Rect(0, 0, 200, 200))
green := color.RGBA{0, 100, 0, 255}
draw.Draw(rectImage, rectImage.Bounds(), &image.Uniform{green}, image.ZP, draw.Src)
file, err := os.Create(rectangle)
if err != nil {
log.Fatalf("failed create file: %s", err)
}
png.Encode(file, rectImage)
}
Most Helpful This Week
Pass different types of arguments in variadic function
Replace first occurrence of string using Regexp
How to delete or remove element from a Map?
How to set timeout for http.Get() requests in Golang?
Sierpinski triangle in Go Programming Language
Regular expression to extract DNS host-name or IP Address from string