The Problem
JavaFX's default way of getting an image from the Windows clipboard,
Clipboard.getSystemClipboard().getImage();
, appears to be broken.
Something seems to go wrong with the transparency of the image. Set on a black background, the image appears fine, but set on a white background, nothing shows at all.
You can test the clipboard using this Minimal, Complete, Verifiable example.
Environment: Windows 7, Java 8 update 202
What I Know
Below, I'll describe the things I already know.
There are Other People who Have Asked Similar Questions:
- Getting Image from Clipboard Awt vs FX
- Image from clipboard not correctly displayed in JavaFX 8 application
Yet, no one has gotten at the heart of the problem or received an answer.
No Official Bug
I can't seem to find a bug regarding this issue in the Java Bug Database.
No Problem for AWT
This problem with images doesn't occur on the AWT clipboard, but I want a solution that uses the JavaFX clipboard.
The Clipboard Contains Multiple Formats
I know that the Windows clipboard contains multiple versions of the same thing, just in different formats. This is easy to see using InsideClipboard or Free Clipboard Viewer.
The JavaFX Clipboard recognizes certain formats; sometimes it has different names for them. application/x-java-rawimage
is what Java considers an image; in code you refer to this as DataFormat.IMAGE
.
I suspect that the DIB
clipboard format in Windows matches up with Java's application/x-java-rawimage
, but can't find proof of that in the source code.
The Problem is Wide-Spread
Problem Applications
JavaFX seems to have this same transparency problem with various applications that copy an image to the clipboard:
- Adobe Reader (from a PDF with images)
- Foxit Reader (from a PDF with images)
- Microsoft Word 365 (from a
.docx
file with images) - Windows 7's Paint (
.png
,.jpg
,.gif
,.bmp
) - Greenshot (which is an enhanced screenshot utility)
- Firefox 65.0.2 (copying the Google.com logo)
Applications Without The Issue
I've also found some applications that copy an image to the clipboard and JavaFX can pull it out using the default method no problem:
- Paint.net
- The PrtScn button
- The Windows Snipping Tool
- Google Chrome 72.0.3626.121 (copying the Google.com logo)
Answering The Question
An adequate answer should
- explain simply, specifically, and with examples why the problem occurs with some applications, but not others, and
- figure out where things go wrong in the JDK implementation and show specifically how to fix said implementation for the majority of the problem applications without breaking it for the applications that already work.
- If it's not feasible to change the JDK's implementation, an adequate answer will provide a Minimal, Complete, and Verifiable example showing JavaFX code that produces an
Image
from the JavaFX clipboard when an image is copied from Adobe Reader.
- If it's not feasible to change the JDK's implementation, an adequate answer will provide a Minimal, Complete, and Verifiable example showing JavaFX code that produces an
If you can't help, but think this is a well researched question, consider voting, or sharing it with a friend.