Save Graphics2D to a jpeg or gif file...

Hi.
I'm trying to save a drawing in a image file format, like jpeg or gif. But i don't have a clue how to do that.
Can any one help? Urgent!!
Thanks!
M�rio Dagot

import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.*;
     // in paint
     BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
     Graphics2D biContext = bi.createGraphics();
     biContext.drawImage(image, 0, 0, null);
     saveAsFile(bi);
public void saveAsFile(BufferedImage img) {
  //Create a fileoutputstream.
  FileOutputStream fos;
  try {
        fos = new FileOutputStream(
              "c:\\nameOfImageFileToSave.jpg");
        JPEGImageEncoder encoder =
          JPEGCodec.createJPEGEncoder(fos);
        encoder.encode(img);
        fos.flush();
        fos.close();
        catch(FileNotFoundException e) {
             System.out.println(e);
        catch(IOException ioe) {
             System.out.println(ioe);
}//end saveAsFile

Similar Messages

  • Can't save TGA and GIF files with layers

    Hi,
    I'm using Photoshop CS5 for Mac, and when I try to save files as TGA or GIF, the Layers option is not available and the result is a flattened, single layer.
    • Files I am working with are 8bit RGB.
    • Photoshop is set to 32bit, not 64bit.
    • There are no extra channels other than RGB.
    • Switching files to indexed color doesn't help because it asks to merge all layers.
    • I can open Gif files containing layers, but I can't then Save or Save As with layers.
    All help appreciated, and thanks in advance.
    Below is the save dialog box as it appears when i try to save a Gif or TGA.

    GIF does not support layers.
    Animated GIF files may open as layers into Photoshop. You need to save them again as an animated GIF file after manipulating them in the animation panel. The 'Save for Web' function would be most ideal.

  • ANIMATIONS (.GIF FILES) AND LAYERS

    1. I HAVE PSE7 AND I CAN'T SEEM TO GET ANY ANIMATIONS TO WORK. IF I OPEN AN ANIMATED (.GIF) FILE, IT IS NO LONGER ANIMATED. DO I HAVE TO PUT IT ON A SPECIAL LAYER TYPE? WHEN I OPEN ONE OF THESE FILES, PSE7 CREATES 3 LAYERS FOR THE IMAGE, NONE OF THEM ANIMATED. WHEN I COMBINE THE LAYERS SO I HAVE ONE LAYER WITH THE IMAGE, IT ASKS IF I WANT TO DELETE THE HIDDEN OR EMPTY LAYERS, CAN'T REMEMBER WHICH.
    2. WHAT IS AN INDEX LAYER? WHEN I OPENED ONE OF MY FILES, IT CAME IN AS AN INDEX LAYER AND I COULDN'T DO ANYTHING WITH IT.
    I'M RUNNING WINDOWS XP PRO SP2.
    THANKS FOR ANY HELP YOU CAN GIVE ME.
    MAUREEN

    GIF does not support layers.
    Animated GIF files may open as layers into Photoshop. You need to save them again as an animated GIF file after manipulating them in the animation panel. The 'Save for Web' function would be most ideal.

  • How can I reduce the size of a gif file?

    Hello, I'm trying to save a 88 layer image as .gif file. there are two questions I have:
    1. I'm trying to resize the image but when I try to save it as .gif the colors are just messed up and greyish. how can I resize it without messing up the pixel colors?
    2. What's the best way to reduce the file size? Is it the dimension of the image? is it the number of colors used in it? or there is another options that i need to manipulate to get a better result?
    thanks in advance.

    The thing is, my friend is using imageready and he says his files have about 150 layers but only 2-3MB
    You need to understand how GIF compression works. It doesn't matter whether you have 1 or 500 layers, 20 or 500 frames of animation. Hell, even the image dimensions do not necessarily matter. The only determining factor for file size is how many colors are spread across the size of the image or the frames, respectively, and those, as the others said can never be more than 256. after that, it is down to how many contiguous areas there are in the image and how large they are - large solid areas are better to compress than many small areas as the overhead increases. I suggest you give these things some consideration first before blindly pushing buttons, which - no offense - is what you seem to be doing right now. You are guessing what might look good, but don't understand how to control it to make it better.
    Mylenium

  • Tracing a .gif file to convert to a Vector graphic in Photoshop CS5 Extended

    I have been on Photoshop CS3 for a couple of years, and now at work they upgraded to CS5 Extended. I am having some small problem finding some things I used to do a lot. I really need to take a bitmap, JPEG, or .gif file and "trace" it and make it into a scalable vector graphic. This used to be easy with Live Trace in CS3. But now on CS5, Live Trace seems to be nowhere to be found.
    What gives?

    I think Live trace is in Illustrator, not Photoshop.
    (I still use Freehand.  )
    Edit: Snap!

  • Why after latest update today my firefox always see GIF files as MP4? Any idea to revert it?

    Right click on GIF image shows "save video as.." no choice to save image as.. there is a "save snapshot as " choice, but no gif file type can be chosen. anyone know how to fix this? before update it worked greatly...

    Try to rename (or delete) the mimeTypes.rdf file in the Firefox profile folder to reset all file actions.
    *http://kb.mozillazine.org/mimeTypes.rdf
    *http://kb.mozillazine.org/File_types_and_download_actions#Resetting_download_actions

  • Jpeg and gif images are not fuzzy

    How can I import jpeg and gif files so that they are crisp and not fuzzy?  I have Lifecycle Designer 7.0
    T

    Look at the bottom of this page as well [http://carlback.blogspot.com/2007/12/apex-and-3rd-party-js-libraries.html]
    Patrick Wolf
    Carl,
    the only drawback with #WORKSPACE_IMAGES# and #APP_IMAGES# is that it doesn't get cached by the browser. So each page request will transmit the hole file again. A caching option for the "Static Files" in the Shared Components would be nice :-)
    About lazy loading with the #WORKSPACE_IMAGES#. I could think about a mod_rewrite rule which translates the additional file request of the JS library into a valid request for APEX.
    Carl Backstrom
    In 3.1 we emit a last modified header now for uploaded files so that should help with browser caching.
    I wonder if anything's changed since then.
    Kofi

  • I'm using OSX 10.7.4 how do I convert gif files to jpeg? Preview does not have a "save as" choice.

    I'm using OSX 10.7.4 how do I convert gif files to jpeg? Preview does not have a "save as" choice.

    GraphicConverter (the best shareware ever devised for the Mac) will do this for you:
    http://www.lemkesoft.com/

  • PSE 7 - Save for Web - Now Defaults to GIF - Prefer JPEG - Can I change it?

    When using PSE 7.0 File > Save for Web, I generally want the files to be JPEGs. In previous versions, the file format on this menu would stay at JPEG once you set it there. Now, each time I open the menu it returns to GIF. Is there a way to have it default or hold as a JPEG in that field?
    Thank you!
    Beverly

    Is there a competing program that is as good as PSE that I could try? I am having a few annoying problems with this program and I think it is time to try something else. I was really excited to use the Photoshop family of photos, but these three problems are getting me down:
    1) I hate that the save for web function defaults to .gif. It is really annoying. The fact that they have a tool to fix it yet that doesn't even work makes me even more bummed out.
    2) When I try to go from the organizer to the editor, it literally takes about 20 seconds for the photo to load. That is a big waste of time.
    3) Sometimes, when I am in the quick edit mode, the zoom feature doesn't work. Other times, the cropping feature doesn't work. I click it back to the full edit, and those functions work. It is odd.
    It isn't like I am trying to split the atom here. I am relatively computer savvy, and all I want to do is edit photos. I have had many problems like this with the previous version as well as the latest one.

  • Save Applet as a jpeg/gif image

    I wrote a small applet to draw some lines.
    it work fine.
    What i want to know is how can i save this applet to a jpeg/gif file.
    (Assume there is a Button in the Applet or Even in the HTML page; when click the button needs to save the applet as an image file)
    is it possible??
    import java.applet.*;
    import java.awt.*;
    public class Test extends Applet {
       int width, height;
       public void init() {
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       public void paint( Graphics g ) {
          g.setColor( Color.green );
          for ( int i = 0; i < 10; ++i ) {
             g.drawLine( width, height, i * width / 10, 0 );
    <applet code="Test" width="300" height="300">
    </applet>
    Message was edited by:
    jugp

    After finding out some facts i tried to do like this;
    but still having some runtime exceptions....
    need help from anybody!!!
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.io.IOException;
    public class Test extends Applet implements ActionListener{
       int width, height;
    Image backbuffer;
       Button save;
       public void init() {
          save = new Button("save");
          add(save);
          save.addActionListener(this);
               width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       public void paint( Graphics g ) {
          g.setColor( Color.green );
          for ( int i = 0; i < 10; ++i ) {
             g.drawLine( width, height, i * width / 10, 0 );
       public void actionPerformed(ActionEvent e) {
         String cmd = e.getActionCommand();  
         if (cmd.equals("save")){
                 BufferedImage bufferedImage = new BufferedImage ( width, height,
                       BufferedImage.TYPE_INT_BGR  );
                        bufferedImage.createGraphics().drawImage( backbuffer, 0, 0, this);
                   try {
                        ImageIO.write( bufferedImage, "jpg", new File ( "image.jpg" ) );
                   } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
    <applet code="Test" width="300" height="300">
    </applet>
    */

  • Photoshop CS5 save animation as jpeg - not gif?

    My content management system will only allow jpgs for this particular section of my site that I'm working in. I have PS CS5 Extended and I already created my animation (basically just a slideshow) and now I need to save it as a .jpg.
    I tried doing Save for web and devices, selecting GIF as the format and then changing it to a jpg extension. This preserves the animation and also gives it the file type that I need. However the gif image quality is awful.
    I remember doing this at point a few years ago but forgot how I did it! Another option was under export, render video I believe, but that just creates hundreds of jpgs and not just one.
    Thanks

    The JPEG format itself does not support animation, though the HTML on your site could change the src attribute of your IMG elements, for example with a script.  I have done exactly that - I have a small Javascript that rotates multiple JPEGs on my own web site (http://www.ProDigitalSoftware.com).  View source to see the simple Javascript.
    Are these large images? Are they photos or drawn graphics?
    You're not going to get good photo quality with GIF because it's an indexed format that can carry at most 256 colors, and if you're spreading those colors across multiple frames then you'll see even further degradation.
    -Noel

  • ImageIO.write to save as GIF file

    I have been use ImageIO.write and BufferedImage to save current Panel's paint(). with transparency.
    BufferedImage image = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    components[0].paint(g2);
    g2.dispose();
    final File file = new File(save_File, "tt.png");
    ImageIO.write(image, "png", file);success at PNG with transparency.
    but now I want to save as GIF file,not png.
    but I change png -> gif
    final File file = new File(save_File, "tt.gif");
    ImageIO.write(image, "gif", file);it will be all black color picture.
    Why?
    And how can I success to do this.

    GIF doen't do well for transparent images. For transparent images, you can find total solutions from this theread, including the reasons why gif doen't work;
    http://forum.java.sun.com/thread.jspa?threadID=5227456

  • When creating a DVD face in Photoshop 13 my save options are limited to .PSE and .PDF.  Why can I not save as a jpeg or other file format?  This was never an issues in Elements 10 (my previous version).

    I just recently purchased Elements 13. I am in the process of creating a DVD face for a video. The design is complete and I would like to be able to save it as a JPEG so I can use it in another program that prints my DVD's. The problem is that it only allows me to save the file as at PSE or a PDF. No other options are available. Why is this?  I upgraded from Elements 10. In Elements 10 I have never had a problem saving this type of project as a JPEG.  Any help would be greatly appreciated.  Thanks!

    ANSWER FOUND!
    Once I finished composing my question I went back and spent some time to figure out the problem. Rather than choosing the "save as" option, which doesn't work, or attempting to choose "export" (grayed out), the correct option is to choose "export creation", which is near the very bottom of the initial drop down options. That works!  I still think it would make sense to have it as an option in the "save as" area.

  • Animated GIF file Creation from no of JPEG files

    Does anybody know the program to convert a many JPEG files to a single animated gif file.

    Oh? And when did you ever say that? Perhaps you should provide clear questions and requirements.
    [http://www.google.com/search?q=animated+gif+java|http://www.google.com/search?q=animated+gif+java]
    Still, learn how to use Google.

  • Problems with Adobe Flash Professional CC (CS6 too) to save animated .gif file or perfect .swf file

    Hello,
    I have a problem with Adobe Flash Professional CC and CS6 to save animated .gif file. Here is very simple work with logo. But I can't save a perfect file swf or gif and it's so bad news for me. Logo .swf file save with bugs. Bugs just see then file insert in freeshop page. Here is just example test page, it let you see logo bugs - http://tagraseshop.graphicsrim.eu . Here is real page, but it's not my, it's freeshop with old logo and I have corect it - http://www.tagras.lt
    Animated .gif file absoluted not saved in Flash CC. Just gif picture. And why? My os Windows 8.1 .  Indeed I try to save on Adobe Flash Professional CS6. It's not save too...
    Gif "animated" - https://mega.co.nz/#!D8pEVaRR!Gjg-nI4T-0vROV9xCa1XfXz4br1X9lpQfUR0nQNT LK4
    Swf with bugs - https://mega.co.nz/#!jo5lSRyS!1x6GjWqYsf7n4KknUs6eF8EqZXEv9e8RsSpGd8h5 jGQ
    Fla - flash document - https://mega.co.nz/#!C94nCDSS!7yKF9FIBvYtVC9r8kYiJ0l0xzUm_R2HwgoXks6iM zUA
    Perfect animation speed is here - http://e-shoptagras.graphicsrim.eu
    Who can help to me?
    Any ideas?
    Thanks for your help in advance!

    Thanks, but I use publish/export settings with GPU hardware acceleration and flash version 11.2 (and 11.8) but nothing happening.
    And why gif file is not all transparenty then I insert on lightblue background? It can't export good quality? Also I use Photoshop, After Effects, Adobe Edge Animate. And nothing happening. Egde animate file .oam is not good for old website navigation. It can't be inserted in freeshop site from navigation... I use logo file .oam just for example here - http://e-shoptagras.graphicsrim.eu
    In reality I want swf file, but we don't find why logo flickering? here http://tagraseshop.graphicsrim.eu
    All file's are transparenty - https://mega.co.nz/#!f8B2CD5R!J8uCJt9Sdq4Suz4HLhTHz8bzJcSWXMBIKcKZgkD3z8c
    I need help! Please!

Maybe you are looking for