-6

Possible Duplicate:
Java Swing : Obtain Image of JFrame

I Want to take the screenshot of jpanel invisibly how do i do .

i don't have idea please let me know.

Community
  • 1
  • 1
DK88
  • 5
  • 2
  • 3
    See [ComponentImageCapture.java](http://stackoverflow.com/questions/5853879/java-swing-obtain-image-of-jframe/5853992#5853992). – Andrew Thompson Jun 27 '12 at 13:18

1 Answers1

2

I am not sure what do you mean by 'invisibly'.Check this whether it is of any help.

In order to save snapshots from java.awt.Components into JPEG or any other format files, you simply:

  • Create a BufferedImage with the same dimensions as those of your Component.
  • Draw the contents of the Component into the BufferedImage.
  • Save the BufferedImage out to a file using the JPEG package and standard FileOutputStream.

void getSnapShot(JPanel panel ){  
       BufferedImage bufImg = new BufferedImage(panel.getSize().width, panel.getSize().height,BufferedImage.TYPE_INT_RGB);  
       panel.paint(bufImg.createGraphics());  
       File imageFile = new File("."+File.separator+snapshotLocation);  
    try{  
        imageFile.createNewFile();  
        ImageIO.write(bufImg, "jpeg", imageFile);  
    }catch(Exception ex){  
    }  
}  
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Gaurav
  • 61
  • 2
  • 5
    1) `}catch(Exception ex){ }` That is not advisable, even in a code snippet. **Do not ignore exceptions.** 2) Images of components are usually better saved as PNG than JPEG (see this [FAQ on screenshots](http://meta.stackexchange.com/questions/99734/how-do-i-create-a-screenshot-to-illustrate-a-post) for further tips). – Andrew Thompson Jun 27 '12 at 13:32
  • 2
    Thanks Andrew.I am new to this place and also new to Java.I have started my career just now and hoping to learn a lot from people here. – Gaurav Jun 27 '12 at 13:45
  • 1
    @Gaurav welcome on this forum, be sure that down_votes are best of ways how to learn something :-) – mKorbel Jun 27 '12 at 13:57
  • 1
    Note also that if you edit the code sample so that it is better, I can not only remove my down-vote and comment, but possibly even up-vote the answer. :) – Andrew Thompson Jun 27 '12 at 14:01
  • in my project i have to attach the image to email so i need to store the image particular folder and attach to mail i don't want to store – DK88 Jun 27 '12 at 14:12
  • 1
    @Gaurav: if you create a Graphics object yourself, as you're doing above, you are also responsible for disposing it, else you risk running out of resources. I also urge you to correct your code above so as not to ignore exceptions. – Hovercraft Full Of Eels Jun 27 '12 at 14:57