bufferedImage有一个getSubimage()方法可以取得一个子图形: public void write(JComponent myComponent, OutputStream out,Rectangel rect) throws Exception
{
int imgWidth = (int)myComponent.getSize().getWidth(),
imgHeight = (int)myComponent.getSize().getHeight();
Dimension size = new Dimension(imgWidth,imgHeight);
BufferedImage myImage =
new BufferedImage(size.width, size.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
//取得子图形
myImage = myImage.getSubimage(rect.x, rect.y, rect.w, rect.h)
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
throw new Exception("GRAPHICS ERROR,CANNOT CREATE JPEG FORMAT");
}
}
{
int imgWidth = (int)myComponent.getSize().getWidth(),
imgHeight = (int)myComponent.getSize().getHeight();
Dimension size = new Dimension(imgWidth,imgHeight);
BufferedImage myImage =
new BufferedImage(size.width, size.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
//取得子图形
myImage = myImage.getSubimage(rect.x, rect.y, rect.w, rect.h)
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
throw new Exception("GRAPHICS ERROR,CANNOT CREATE JPEG FORMAT");
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货