Skip to content

Windows: Write an image encoded to PNG after being loaded as JPEG messes up the image #48

Open
@matwachich

Description

@matwachich
package main

import (
	"bytes"
	"image"
	"image/png"
	"os"

	"golang.design/x/clipboard"

	_ "image/jpeg"
)

func main() {
	if err := clipboard.Init(); err != nil {
		panic(err)
	}

	// open some jpeg file
	fh, err := os.Open("image.jpg")
	if err != nil {
		panic(fh)
	}
	defer fh.Close()

	// decode the jpeg
	img, _, err := image.Decode(fh)
	if err != nil {
		panic(err)
	}

	// re-encode it to png
	var buf bytes.Buffer
	if err := png.Encode(&buf, img); err != nil {
		panic(err)
	}

	// image copied to clipboard is totally messed up
	clipboard.Write(clipboard.FmtImage, buf.Bytes())

	// image saved to file is good
	wfh, err := os.Create("image.png")
	if err != nil {
		panic(err)
	}
	defer wfh.Close()

	wfh.Write(buf.Bytes())
}

When executing this on windows, the PNG on disk is good, but the one on clipboard is bad

Source JPEG
508118

Here is the resulting PNG in clipboard
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions