Results 1 to 3 of 3
  1. #1
    archanajathan is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default copy image/imageicon into a file on disk

    I have created an imageIcon. I want to copy it into a file on the disk.


    I have file on my disk (named : abc.jpg). I picked up the file and stored it in an imageicon using the code :

    ImageIcon i=new ImageIcon("abc.jpg");


    Then i did some processing on the imageicon "i" using the method :

    ImageIcon process(ImageIcon)


    The process method returned an imageicon which i stored into the variable "new_i" of type ImageIcon like this:

    ImageIcon new_i=process(i);


    Now i want to store this new_i ImageIcon into the disk (with named suppose : new_abc.jpg)


    How do i do that?
    Well, i tried it out after a lot of research in this way :

    Image img = new_i.getImage();

    BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(nul l), BufferedImage.TYPE_BYTE_ARGB );

    ImageIO.write(bi, "jpg", new File("new_abc.jpg"));


    The above code creates the file "new_abc.jpg" and just copies the structure of the image icon. From structure, i mean to say, it creates a black image with the size same as "abc.jpg".

    If you can identify the mistake in the above code then please let me know. Or else if you know some other method for copying an image into a file, the please reply.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    Image img = new_i.getImage();
    
    BufferedImage bi = new BufferedImage(img.getWidth(null),img.getHeight(null,
                                         BufferedImage.TYPE_BYTE_ARGB);
    Graphics2D g2 = bi.createGraphics();
    // Draw img into bi so we can write it to file.
    g2.drawImage(img, 0, 0, null);
    g2.dispose();
    // Now bi contains the img.
    // Note: img may have transparent pixels in it; if so, okay.
    // If not and you can use TYPE_INT_RGB you will get better
    // performance with it in the jvm.
    ImageIO.write(bi, "jpg", new File("new_abc.jpg"));

  3. #3
    archanajathan is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Smile

    hey thanks for the instant and correct reply...it worked.:)

Similar Threads

  1. how to save image to disk after using pixelgrabber
    By shishirg in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 03:45 PM
  2. Replies: 2
    Last Post: 10-05-2008, 07:13 PM
  3. Disk File System Access and Monitoring
    By drveresh in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2007, 03:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •