Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matwachich opened this issue Jun 7, 2023 · 5 comments
Labels
invalid This doesn't seem right

Comments

@matwachich
Copy link

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

@changkun
Copy link
Member

changkun commented Jun 8, 2023

According to the doc of Write: Write writes a given buffer to the clipboard in a specified format. Write returned a receive-only channel can receive an empty struct as a signal, which indicates the clipboard has been overwritten from this write. If format t indicates an image, then the given buf assumes the image data is PNG encoded.

@changkun changkun added the invalid This doesn't seem right label Jun 8, 2023
@matwachich
Copy link
Author

Euh yes, I don't understand yet what I did wrong in my code.

I decode a jpeg into an image.Image, then I re-encode it to PNG in a bytes.Buffer, and I write this buffer to the clipboard and to a file to compare.

The written file is good, but clipboard's content is screwed.

@changkun
Copy link
Member

changkun commented Jun 9, 2023

Which clipboard receiver are you using on Windows? Are you using the system built-in clipboard? (On Windows, use Shift+Win+s) I remember from Windows; it might be problematic because there is an implicit agreement between the content writer and content receiver.

@matwachich
Copy link
Author

matwachich commented Jun 10, 2023

I just past clipboard content inside paint.exe.
I don't use any non standard clipboard.

I think the implicit agrement is that the data is a PNG encoded data/file?

@thewh1teagle
Copy link

thewh1teagle commented Feb 25, 2024

I experience the same issue, no matter what image I use, and if it's png encoded or not, the final image which I paste from the clipboard is corrupt. if I write it to file instead, the image is normal png image.
I guess that windows expects for BMP and not PNG or someting like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants