Comments (10)
It seems like doing this makes image writing works on Linux Ubuntu, even without triggering changed
. Why does this happen?
And this will block the program.
changed := clipboard.Write(clipboard.FmtImage, sampleImgBuf)
select {
case <-changed:
println(`Image Updated`)
}
from clipboard.
On Linux, you must keep the application running so that others can read your data. See #15 for another discussion. Closing because this is not a bug, and feel free to reopen the issue if this is actually a bug.
from clipboard.
@changkun Thanks, I kind of noticed that the program has to be running. I did a work-around with xclip.
Is it the same on Windows? I also can't write image on Windows.
from clipboard.
That's not a requirement on Windows. Is the problem reproducible on latest release?
from clipboard.
@changkun Yes I tried to write an image on Windows on the latest release and it doesn't work.
Here is my sample code.
package main
import (
"encoding/base64"
"golang.design/x/clipboard"
"log"
)
func main() {
if err := clipboard.Init(); err != nil {
log.Fatal(err)
}
base64Str := "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAgACADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDumIVSx6AZrD0LxPba9d3sFujD7K+xie5rVvpPJsJ5P7qE/pXn3woXdY6rfdWluWNcEYpxbOlvVI9JormvDPioa/eahbGHy3tJNh966WpaadmUnfYy/ESzP4fvVgUtK0TBQOpOK8s8Kaj4q8P6MbC38PPJlifMbjOa9npMD0FXGdlaxLjd3ueFeDr/AMTReJtRjsrSPzpJd1yr/wAHPavc4S5hQyDD4G761mWXh6xsNWudSgQrPcffPY1rUVJqT0CEWkf/2Q=="
imgBuf, err := base64.StdEncoding.DecodeString(base64Str)
if err != nil {
log.Fatal(err)
}
clipboard.Write(clipboard.FmtImage, imgBuf)
}
from clipboard.
And if I use select
to monitor changed
, there is a deadlock.
changed := clipboard.Write(clipboard.FmtImage, imgBuf)
select {
case <-changed:
println(`"text data" is no longer available from clipboard.`)
}
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive (nil chan)]:
main.main()
C:/Users/username/Desktop/clipboard-app/main.go:20 +0xce
from clipboard.
Is your image png encoded?
from clipboard.
@changkun Yes, if you copy the base64 string to https://onlinepngtools.com/convert-base64-to-png
The image can be rendered.
from clipboard.
Wait, it seems like it's not png encoded even if it can be displayed.
I tried to take a screenshot and read it to string with go clipboard, then write it back. It works!
So does this mean other type of image encoding are not supported?
I will experiment a few more things, thanks!
from clipboard.
As documented in the API, the image buffer must be png encoded: https://pkg.go.dev/golang.design/x/clipboard#Write
It is recommended that you encode it using image/png package to encode the bytes buffer
There are underlying implementation reasons, but it is not important for users who are not interested in the details.
from clipboard.
Related Issues (20)
- can't work in the win service HOT 5
- Build fails with "error: implicit declaration of function 'nanosleep'" HOT 1
- PNG data overwritten on Windows HOT 4
- How to watch copy file in windows10 HOT 3
- First thanks for your contribution. Maybe it's a stupid question, may i ask why can`t copy jpg image HOT 1
- Feature Request: Support to read and write raw content (without any formatting) from and into clipboards
- Housekeeping: update golang version and library dependencies HOT 2
- Windows 32-bit system will get stuck when executing. HOT 3
- Windows: Write an image encoded to PNG after being loaded as JPEG messes up the image HOT 5
- TestClipboardMultipleWrites only write text,no image. HOT 1
- The shortcut key Ctrl+C cannot be detected when copying images. HOT 4
- Remove cgo requirements on macOS HOT 2
- suggestion: making `Write` blocking instead of returning a channel HOT 30
- Add *BSD support
- Hangs / slow on Linux
- Don't panic when headless HOT 1
- Is `clipboard.Write(clipboard.FmtImage, []byte)` not design to copy any other format than `PNG`? HOT 1
- 可以做成像 electron 那样,同时写入多种内容么,例如同时写入 text 和 html
- Crashing on wayland
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from clipboard.