当前位置: 技术问答>java相关
能否只存储Graphics所画图形的一部分???
来源: 互联网 发布时间:2015-10-18
本文导语: 如题 | bufferedImage有一个getSubimage()方法可以取得一个子图形: public void write(JComponent myComponent, OutputStream out,Rectangel rect) throws Exception { int imgWidth = (int)myComponent...
如题
|
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");
}
}
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");
}
}