conversion of a Object(e,g Canvas or Panel) in to image of Gif or Jpg format

I want to convert a object(e,g Panel or Canvas) along with any thing which has drawn on it, into Gif/Jpg image.
nadirkamalAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MurthyMvlnCommented:
Any component can be converted to GIF/jpg image. This we can convert
using GIFEncoder( third party tool ).
This GIFEncoder utilities can be downloaded from www.acme.com.
After downloading this, import those classes to your file and write down the following code.

FileOutputStream fos =
  new FileOutputStream(filePathName);

Image img = this.createImage(
                   this.getWidth(),
                   this.getHeight());
//Here this will be any component
GifEncoder gifEncoder = new GifEncoder(
                img,fos);              
gifEncoder.encode();
fos.flush();
fos.close();

If any problems mail me back.

Murthy Mvln
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MurthyMvlnCommented:
I hope, i given the correct solution for your problem.
0
siayubiCommented:
Dear MurthyMvln

GifEncoder only has the following Constractor

GifEncoder(Image, OutputStream)
GifEncoder(Image, OutputStream,boolean)
GifEncoder(ImageProducer, OutputStream)
GifEncoder(ImageProducer, OutputStream, boolean)

Your "fos" is not a OutputStream, it is FileOutputStream so how it can initiallize

Waiting for response.....

Sohail Iqbal
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

siayubiCommented:
Sorry!

Please also told me that what is filePathName?
Is it is File or String?
0
siayubiCommented:
I have use your code, it has compile correctly but give NoClassDefFondError
Exception.

Sohail Iqbal
0
MurthyMvlnCommented:
1) Please also told me that what is filePathName?

Is it is File or String
Here the filePathName is the File Path where you want to store it.
eg. File filePathName = new File("../siayubi.gif");

2) I have use your code, it has compile correctly but give NoClassDefFondError
Exception.

check out where your class is created. when ever you are running your programme it is not able found the class.

eg. with out existing Temp.class, if we are trying to execute that( ie. java Temp ) then this Exception will come.
so check out for your class file.

0
MurthyMvlnCommented:
1) Please also told me that what is filePathName?

Is it is File or String
Here the filePathName is the File Path where you want to store it.
eg. File filePathName = new File("../siayubi.gif");

2) I have use your code, it has compile correctly but give NoClassDefFondError
Exception.

check out where your class is created. when ever you are running your programme it is not able found the class.

eg. with out existing Temp.class, if we are trying to execute that( ie. java Temp ) then this Exception will come.
so check out for your class file.

0
siayubiCommented:
Dear MurthyMvln

I think that the Exception is comming due to the following reason....

GifEncoder only has the following Constractor

GifEncoder(Image, OutputStream)
GifEncoder(Image, OutputStream,boolean)
GifEncoder(ImageProducer, OutputStream)
GifEncoder(ImageProducer, OutputStream, boolean)

Your "fos" is not a OutputStream, it is FileOutputStream so how it can initiallize

Waiting for response.....

Sohail Iqbal
0
MurthyMvlnCommented:
OutputStream is the super class for FileOutputStream. If you are passing the FileOutputStream, it will convert to OutputStream with out any problem.
so that is not the problem. please check what ever i mentioned before.

0
siayubiCommented:
Dear MurthyMvln

I am sending you all my code please compile it and see the Exception.

Please where the problem is....
and also send me you email address.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import Acme.JPM.Encoders.*;
import java.io.*;
import java.net.*;

public class Converter{
  TextCanvas canvas;
  public Converter(){
    File filePathName = new File("../siayubi.gif");
    canvas = new TextCanvas();
    canvas.createImage(filePathName);
  }
      public static void main(String args[]){
            System.out.println("Sohail Iqbal");
    new Converter();
      }
}

class TextCanvas extends Canvas{

      Graphics       g;

      TextCanvas(){
            g=getGraphics();
            setBackground(Color.blue);
            setFont(new Font("Arial",Font.BOLD,28));
      }

      public void createImage(File filePathName)
      {
             try{
               FileOutputStream fos =  new FileOutputStream(filePathName);
               Image img = this.createImage(this.getWidth(),this.getHeight());
               //Here this will be any component
               GifEncoder gifEncoder = new GifEncoder(img,fos);
               gifEncoder.encode();
               fos.flush();
               fos.close();
            } catch (IOException ignored){}
      }

      public void paint(Graphics g){
            g.drawString("Sohail Iqbal", 40,40);
      }
}


Best Regards
Sohail Iqbal
0
siayubiCommented:
Dear Murthy Mvln

Whenerver I run this code it has given the NullPointerException.
Please help me I will be very thankful to you.

Best Regards
Sohail Iqbal
0
MurthyMvlnCommented:
Hi Sohail!

Here TextCanvas extending Canvas. You are taking the instance of this TextCanvas. But you are not adding to any Panel or Frame. You should add the Canvas to Panel or Frame. Then only it will go to paint method.

please find the difference here.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import com.Printing.JPM.Encoders.*;
import java.io.*;
import java.net.*;

public class Converter extends Frame
{
  TextCanvas canvas;
  public Converter()
  {
     setVisible(true);
     setSize(100,100);
     canvas = new TextCanvas();
     add(canvas);
     canvas.createImage();
  }
  public static void main(String args[])
  {
     new Converter();
  }
}

class TextCanvas extends Canvas
{
   TextCanvas()
   {
        setSize(100,100);
        setVisible(true);
        setBackground(Color.blue);
        setFont(new Font("Arial",Font.BOLD,28));
   }

   public void createImage()
   {
     try
     {
           OutputStream fos =  new FileOutputStream("c:/Temp.gif");
           Image img = this.createImage(this.getWidth(),this.getHeight());
           GifEncoder gifEncoder = new GifEncoder(img,fos);
           gifEncoder.encode();
           fos.flush();
           fos.close();
      }
      catch(Exception e)
      {
         System.out.println("Error: "+e.toString());
      }
    }

    public void paint(Graphics g)
    {
        g.drawString("Welcome to GifEncoder", 40,40);
    }
}

This code is executing correctly.

still problems mail me to
murthy@claygency.com

MurthyMvln
0
nadirkamalAuthor Commented:
Thanks from Sohail Iqbal
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Graphics Software

From novice to tech pro — start learning today.