copy a part of component image

Is there any way to get part of a component image ? This is the way to copy entire component image to a buffered image :

  Comp.paint(bi.createGraphics());

If I do this, I have to create a large buffered image and then cut off the part of image that I need. I wonder if it is possible to only copy the part of a component image to small buffered image ?

Jay

create a BufferedImage that is the size of the area you want to capture, then alter the BIs Graphics object so it has the correct translation.

For example,
if you want to capture the rectangle (x,y,width,height) of a given component, you would create a BufferedImage with dimensions (width,height) and translate the BIs Graphics context (-x,-y), then pass it into the components paint method.

Thanks for the response. But how do you pass an affinetransform to paint ? I must have done something wrong :

public static BufferedImage partOfImage(JComponent comp, int x, int y, int w, int w)
{
BufferedImage bimage;
AffineTransform atX=new AffineTransform();
bimage=gc.createCompatibleImage(w,h,Transparency.TRANSLUCENT);

  Graphics2D g=(Graphics2D) comp.getGraphics();
            atX.translate(-x,-y);
  g.setTransform(atX);
  comp.paint(bimage.createGraphics());
  return bimage;

}

This does not give what I need. I probably need to pass atX to paint not via g.setTransform(atX).

Thanks

Jay

Noooooo, u need to alter the Graphics context you get from the BufferedImage :-


BufferedImage bimage = gc.createCompatibleImage(width,height,Transparency.TRANSLUCENT);
Graphics g = bimage.createGraphics();
g.translate(-x,-y); //im using the origin translation rather than an AffineTransform - its quicker.
comp.paint(g);
return bimage;

Yes, you are the best. Thanks.