开发者

Error transfering image using Clipboard, Toolkit and Transferable

I get this error message from the following code:

Exception in thread "main" java.lang.IllegalArgumentException: Width (-1) and height (-1) cannot be <= 0
  at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:999)
  at sun.awt.datatransfer.DataTransferer.imageToStandardBytes(DataTransferer.java:1994)
  at sun.awt.windows.WDataTransferer.imageToPlatformBytes(WDataTransferer.java:267)
  at sun.awt.datatransfer.DataTransferer.translateTransferable(DataTransferer.java:1123)
  at sun.awt.windows.WDataTransferer.translateTransferable(WDataTransferer.java:163)
  at sun.awt.windows.WClipboard.setContentsNative(WClipboard.java:73)
  at sun.awt.datatransfer.SunClipboard.setContents(SunClipboard.java:93)
  at automateSignature.LoadToClipboard.main(LoadToClipboard.java:8)

What code changes, or changes to the image file, are needed to fix this error? Note that this code was contributed by Oscar Reyes, but that any errors are mine.

import java.awt.*;
import java.awt.datatransfer.*;

public class LoadToClipboard {

    public static void main(String[] args) {
        Toolkit tolkit = Toolkit.getDefaultToolkit();
        Clipboard clip = tolkit.getSystemClipboard();
        clip.setContents(new ImageSelection(
            tolkit.getImage("MKSignature.jpg")), null);
    }
}

class ImageSelection implements Transferable {

    private Image image;

    public ImageSelection(Image image) {
        this.image = image;
    }

    // Returns supported flavors
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[]{DataFlavor.imageFlavor};
    }

    // Returns true if flavor is supported
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return DataFlavor.imageFlavor.equals(flavor);
    }

    // Returns image
    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
        if (!DataFlavor.imageFlavor.equals(flavor)) {
            throw new UnsupportedFlavorException(flavor);
        }
        return image;
    }
}

I have tried to find a place in the code where width and height can be specified, but have not succeeded. I also examined the properties of the jpg file and the w 开发者_如何转开发and h are specified.enter code here

NEW ATTEMPT

I have NOW changed the code to the following: Note the use of createImage() and prepareImage(). The width and height errors are now fixed.

public static void main(String[] args) {
    Toolkit tolkit = Toolkit.getDefaultToolkit();
    Clipboard clip = tolkit.getSystemClipboard();
    //File file = new File(
    //  "C:\\aaaa\\Admin\\SIGNATUREForInsertionIntoDocs\\MKSignature.jpg");
    signatureImage = tolkit.createImage(
        "C:\\aaaa\\Admin\\SIGNATUREForInsertionIntoDocs\\MKSignature.jpg");
    tolkit.prepareImage(signatureImage, 108, 60, null);
    // JOptionPane.showConfirmDialog(null, "Press to continue");
    ImageSelection i = new ImageSelection(signatureImage);
    clip.setContents(i, null);
}

However the image that is now placed in the clipboard is not the same as the image in the jpg file. It is the same in size, but not in content.

The image as displayed in GIMP (from the .jpg file) is a piano.

The image pasted from the clipboard after running the above program to set the clipboard content, is the same size, but is blank!

Somehow the image has been stripped of its content. What am I doing wrong?


Have a closer look at the toolkit method Used to read in the image. You most likely want a synchroneus method Reading in an icon.


I have rewritten the program using code from ExampleDepot.com:

package source;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageFromFileToClipboard {

    public static void main(String[] args) {
        Image image = null;
        Image imageFromClipboard = null;
        try { 
            File file = new File("C:\\Documents and Settings\\Mike King\\My Documents\\My Pictures\\PianoSmallThumbnail.jpg"); 
            image = ImageIO.read(file);
        }
        catch (IOException e) { 

        } 

        // Use a label to display the image 
        JFrame frame = new JFrame(); 
        JLabel label = new JLabel(new ImageIcon(image)); 
        frame.getContentPane().add(label, BorderLayout.CENTER); 
        frame.pack(); 
        frame.setVisible(true); 

        System.out.println(image.toString());

        setClipboard(image);

        imageFromClipboard = getClipboard();
        // Use a label to display the image 
        JFrame frame2 = new JFrame(); 
        JLabel label2 = new JLabel(new ImageIcon(imageFromClipboard)); 

        frame2.getContentPane().add(label2, BorderLayout.CENTER); 
        frame2.pack(); 
        frame2.setVisible(true); 
    }

    // If an image is on the system clipboard, this method returns it; 
    // otherwise it returns null. 
    public static Image getClipboard() { 
        Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 
        try {
            if (t != null && t.isDataFlavorSupported(DataFlavor.imageFlavor)) { 
                Image text = (Image) t.getTransferData(DataFlavor.imageFlavor); 
                return text;
            }
        } 
        catch (UnsupportedFlavorException e) { } 
        catch (IOException e) { } 

        return null; 
    } 

    // This method writes a image to the system clipboard
    public static void setClipboard(Image image) { 
        ImageSelection imgSel = new ImageSelection(image); 
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null);
    } 

    // This class is used to hold an image while on the clipboard.      
    public static class ImageSelection implements Transferable { 
        private Image image; 

        public ImageSelection(Image image) { 
            this.image = image;
        }  

        public DataFlavor[] getTransferDataFlavors() { 
            return new DataFlavor[]{DataFlavor.imageFlavor};
        } 

        public boolean isDataFlavorSupported(DataFlavor flavor) { 
            return DataFlavor.imageFlavor.equals(flavor);
        } 

        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 
            if (!DataFlavor.imageFlavor.equals(flavor)) { 
                throw new UnsupportedFlavorException(flavor);
            }

            return image;
        } 
    }
}

The image from the jpg file is now successfully read and placed in the clipboard. Manually pasting from the clipboard into any doc, functions correctly. The use of JFrame is just to check what was read from the file, and then what was put in the clipboard.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜