Converting JPanels as image objects

hi,
i am having problem in converting a JPanel object which consits of some
JLabels and images into a Image Object. Actually i want to save the JPanel as
a jpg file which i will do with jped encoder.
so please help me in converting this JPanel to image object

assuming the panel is showing.... (if not, you might need to user getPreferredSize() and setVisible before paint()).
Dimension size = panel.getSize();
BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = bi.createGraphics();
comp.paint(g2d);

Similar Messages

  • How do i convert an image object to a byte array ?

    Hi
    how do i convert an image object into a byte array
    early reply apperciated

    Oh sorry my method and the other method need to have the pixels from the Image passed to them which you get my using pixelgrabber:
    //create width and height variables from image
              int w = img.getWidth(this);
              int h = img.getHeight(this);
              //retrive picture from image
              int[] pix = new int[w * h];
              PixelGrabber pg = new PixelGrabber(img, 0, 0, w, h, pix, 0, w);
              try{ pg.grabPixels();
              } catch (InterruptedException ioe) {
                   System.err.println("Interrupted");
              if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
              System.err.println("image fetch aborted or errored");
              }

  • Converting an Image object to a BufferedImage object?

    I have a BufferedImage object, bi, which I want to resize, hence:
    Image image = bi.getScaledInstance(400, 300, Image.SCALE_DEFAULT);
    Next I want to encode the resized image to JPEG format. The problem is that inorder to use the JPEGImageEncoder.encode() method, I need to pass it a BufferedImage object. I have looked through the API and cant see a way of converting the Image object to a BufferedImage object.
    Can anyone shed some light on this problem?
    Any help will be much appreciated.

    I copied + pasted the code from:
    http://java.sun.com/docs/books/tutorial/2d/problems/ind
    x.html :-)
    So you propably could use :
    int width = 400;
    int height = 300;
    Image image = bi.getScaledInstance(width, height,
    Image.SCALE_DEFAULT);
    BufferedImage bi = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_RGB);
    Graphics2D biContext = bi.createGraphics();
    biContext.drawImage(img, 0, 0, null);Greets
    PuceThis works fine; but has anybody else noticed this takes an unreasonable amount of time? How to get around this?

  • Converting a image file (JPG or BMP or any other) to an Image object

    Hello, does anyone have an idea of how I could convert a image file (JPG, BMP, GIF or any other) or even a Corel Draw (.CDR) file to a java Image object?
    Thanks in advance
    Wilson

    Demo:
    import java.awt.image.*;
    import java.io.*;
    import java.net.*;
    import javax.imageio.*;
    import javax.swing.*;
    public class ImageResized {
        public static void main(String[] args) throws IOException {
            URL url = new URL("http://today.java.net/jag/bio/JagHeadshot.jpg");
            BufferedImage image = ImageIO.read(url);
            Icon icon = new ImageIcon(image);
            JLabel label = new JLabel(icon);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(label);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
    }

  • How to convert a Image object to byte Array?

    Who can tell me, how to convert Image object to byte Array?
    Example:
    public byte[] convertImage(Image img) {
    byte[] b = new byte...........
    return b;

    Hello,
    any way would suit me, I just need a way to convert Image or BufferedImage to a byte[], so that I can save them. Actually I need to save both jpg and gif files without using any 3rd party classes. Please help me out.
    thanx and best wishes

  • Problem in converting text to image.

    Hi I am trying to convert a text to image. The code for the same is pasted below. The problem is how do I change the background color. No matter what color I give with Graphics2D object, the background is always black. I want a white background.
    public class TextToImg1
    public static void main(String args[])
    try
    int width = 400;
    int height = 400;
    BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2D = bimage.createGraphics();
    g2D.drawString("This is the sample page to convert text to image.",100,100);
    g2D.setBackground(Color.WHITE);
    g2D.setColor(Color.WHITE);
    File f = new File ("anImage.jpg");
    ImageIO.write (bimage, "jpeg", f);
    catch(Exception exception)
    System.err.println(exception.getMessage());
    }

    Probably because you didn't draw the background. Use the fillRect(...) method to paint the background using the color your want.

  • Screen dump of JPanel to Image

    I'm working on an extensive error dialog which will send all relevant information to a support apparatus. This includes collecting the stack-trace received from the exception, java console log and a screen dump of the underlying JPanel.
    The dialog is a modal dialog residing on top of the JPanel in question. I can retreive the parent panel's Graphics object and need to print the JPanel with all it's children to an Image object.
    This should be possible, but how?
    Another thing, is the java console available from the client code? The code runs as an applet on Windows clients.

    I've never tried this, but the idea I have is to create a BufferedImage, and getGraphics() of the BufferedImage. Now call paint() of the JPanel and pass it the graphics object.
    Maybe you also have to pre-obtain the height/width of the JPanel for the size of the Image.

  • How to save image using an image object in servlet on web server

    I'll be very thankful to anyone who helps me in this matter.
    i developed an applet which draws on a buffered image.
    now i want to save this buffered image as a jpg image on the web server.
    i know i have to use servlets or jsp for this.
    but i want to use servlet for specific reasons.
    can anyone plz provide me the code for taking an image object from an applet and saving it thru servlet.
    i need this solution as soon as possible.
    thanks in advance.

    Take a look around for URLConnection, and Applet to Servlet communication (also check the [url http://forum.java.sun.com/forum.jsp?forum=33]Servlet forum).
    Basic concepts will be to open a URLConnection to the URL mapped on your server for the servlet, then send the image, byte by byte, to the servlet, then ask for a reply from the servlet.
    Once asked for a reply the servlet will need to take the image sent to it (hopefully via an HTTP post) and copy it to a location on disk. (You could have a parameter to the request that tells you want to name the file...)
    As for the converting buffered image to JPEG, I haven't done this before, but I know others have. You can look around (more likely to find it in the [url http://forum.java.sun.com/forum.jsp?forum=31]Java Programing[ul] forum) for the code needed.

  • WDJ convert base64binary to image

    Hi Guys,
    I have an issue where I want to display an image that is stored in a base64binary Business Object attribute on an application service and convert it back to an image on the WDJ view.
    I have tried first displaying the response content as text, what comes back seems to be a relative path i.e. ../../folder1/folder2/image.jpg
    followed by a whole stream of session-id looking numbers.
    That obviously didnt work as it was relative & i tried adding the correct start for the path but that didnt work either.
    I then tried looking at taking the .toString() of the response attribute & setting that as reference for an image. Unfortunately that doesnt work either.
    I'm now stuck with ideas. I know that it is possible as the WS Navigator can provide the file as a download.
    Any ideas or pointers for where to look.
    Also, any thoughts about whether base64binary is the right attribute to be storing the image as.
    Cheers.
    Edited by: Kelsall Mark on May 26, 2010 5:47 PM

    hey Kelsall,
    I have came across a thread on sun java forum [converting base64binary to image|/thread/5161771 [original link is broken];. Hope this would help.
    please let me know status,
    Good Luck,
    Nikhil

  • Get an Image object

    hello :)
    My problem is that, i am trying to draw an image in a JPanel, but i cant get the object (Image) i want to use, cause i dont know if swing has any Image object creator like Applets getImage or Toolkit getImage.. and i want to do this without using Toolkit.getDefaultToolkit().geImage(URL)
    Tank u for your help.

    It is better to load images using .....getClass().getResource(....) i.e.
    ImageIcon i = new ImageIcon(getClass().getResource(url));
    With this approach the url can be CLASSPATH relative and the image can be in a jar (the same
    jar as the class loading it).
    Hope that helps you

  • Outputting an Image object to jpg or png

    Hello,
    I have an Image object in my code that is output from another java object. The other java object is from a libarary of GIS classes known as JLOOX.
    Anyway, I have an Image stored in the Image object but want to output it to a file. I was looking at the Java Almanac and saw a page http://javaalmanac.com/egs/javax.imageio/Graphic2File.html However, this uses a BufferedImage object.
    Is there anything I can do with the Image object to output to a file? Can I convert it to a BufferedImage somehow?

    BufferedImage b = new BufferedImage(
    source.getWidth(), source.getHeight(),
    BufferedImage.TYPE_INT_ARGB  );
    b.getGraphics().drawImage( 0, 0, source, null );
    ImageIO.write( b, "png" );Thanks, but I have an Image object. How do I write the Image object to ImageIo.write?

  • Iphone get photo as image object

    hi,
    I am new to Mac os programming. I am testing iphone programming. I would like to access the photo saved in iphone as an bitmap image object. How can I do it?
    I know I can use UIImagePickerController, but not sure how to get the image object and access every pixel after that.
    Thank you very mcuh.
    af

    The UIImagePickerControllerDelegate method didFinishPickingImage is called once the user chooses a photo. This contains the UIImage. UIImages can be converted
    http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIImageP ickerControllerDelegateProtocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#// appleref/occ/intfm/UIImagePickerControllerDelegate/imagePickerController:didFinishPic kingImage:editingInfo:

  • Pages for iOS converts movies to images on ePub export

    It appears that Pages for iOS is able to use the Apple-supplied template for ePub export that was originally developed for Pages in the iWork '09 suite.  However, when I add video to a document and then export to ePub, Pages for iOS issues this message in the Export Details:
    Some changes were made to your exported documnet.
    Movies were converted to images.
    The movies play fine in Pages for iOS, they just don't get exported when ePub is chosen despite the fact that iBooks for iOS will handle video embedded in ePub perfectly well.
    Since Pages for MacOS X exports to ePub without converting movies to images, this appears to be a major parity issue. Apple has gone to some lengths to reach parity between MacOS X, iOS and iCloud with respect to iWork and iLife app suites and has gotten considerable heat over those efforts, I have to wonder whether I've missed something in coming to this sad conclusion. Have I?

    It sounds as if your caption is beneath the object to which it's referencing. If so, you can either do one of two things. The first thing (and doesn't require any XHTML knowledge) is to place the caption above the image (a bit unconventional in comparision to dead-tree printing). This will cause the e-reader to begin the page with the caption (the linked item) and thus contain the object of reference below, ensuring it's on the same page for viewing.
    Another option, and a more elegant solution (but more technical), is to open the EPUB file with an app like BBEdit or TextMate. Both of these apps can open an EPUB file without having to unzip the file. With the file open, navigate to the file in which you want to make your edit (which will be located in the OEBPS folder). Run a Regex search for your caption title (regex \bcaption\b) and ensure you've found the correct location. Locate your <a id="caption" /> destination anchor. The code just above this anchor should be your image. Remove this code above the image with a p class, like so: <p class="caption"><a id="caption1" />&#160;</p>. This effectively forces the ereader to display the object of reference.

  • Need to convert byte[] to image

    hi frnds,
    i have a byte[](which i got it from an image) i need to convert back to image.Could u pls suggest me how to??
    i tried with Pixelgrabber but i exaclty did get wht shld be the input for it(tried giving image file name). :(
    So pls let me know how and wht to use..ASAP.
    Regards
    subinjava

    Subinjava wrote:
    ..i have a byte[](which i got it from an image) i need to convert back to image.Could u pls suggest me how to??
    Image image = Toolkit.getDefaultToolkit().createImage(byteArray)See http://java.sun.com/javase/6/docs/api/java/awt/Toolkit.html#createImage(byte[]) for more details.

  • Convert a Tiff Image to png and show

    Hello;
    I wanto to convert a Tif image to png and show this in my web page but i dont want to save the png iamge in my server.
    Actualy i do that:
    byte[] bytes = imagenData;
                    ByteArraySeekableStream tiffStrm = new ByteArraySeekableStream(bytes);
                    String[] CdcNms = ImageCodec.getDecoderNames(tiffStrm);
                    ImageDecoder tifDecoder = ImageCodec.createImageDecoder(CdcNms[0], tiffStrm, new TIFFDecodeParam());
    //                 Check the number of pages
                    int IFDs = tifDecoder.getNumPages();
    //                 Get the first page
                    RenderedImage ren = tifDecoder.decodeAsRenderedImage(1);
                    byte[] bytes2 = doSingleConversion(ren);AND
    protected byte[] doSingleConversion(RenderedImage tifImage) throws Exception
              byte[] bytes = null;
              String out = "temp.png";
              try {
                   //      Create the PNG file
                   PNGEncodeParam pngParm = PNGEncodeParam.getDefaultEncodeParam(tifImage);
                   JAI.create("filestore", tifImage, out, "PNG", pngParm);
                   //Read the file into a byte array
                   File f = new File(out);
                   FileInputStream in = new FileInputStream(f);
                   bytes = new byte[(int) f.length()];
                   in.read(bytes);
                   in.close();
                   return bytes;
              catch(Exception e) {
                   e.printStackTrace ();
                   throw(e);
         }But this code save de image in the server; it�s posible do it without save.

    Numbers the iWorks program? Not sure you can convert it to that or just a list of numbers. Never heard of anyone that would even want to convert an Image file, Photo, to any other format. Other then another image format like JPG, PNG or a PSD Photoshop file.

Maybe you are looking for