日期:2014-05-20  浏览次数:20741 次

关于Java Swing panel中 图片保存(某区域)
保存panel中一块区域  保存生图片
代码:
BufferedImage image = new BufferedImage(Main.mp.getWidth(),Main.mp.getHeight(), BufferedImage.TYPE_INT_RGB);   
Graphics2D g2 =(Graphics2D) image.getGraphics(); 
Main.mp.paint(g2); 
save(image);
public void save(BufferedImage bImage)
{
//把图像保存为文件
JFileChooser chooser=new JFileChooser();//文件保存对话框
chooser.setCurrentDirectory(new File("."));
if(chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
{
File oFile=chooser.getSelectedFile();
try
{
ImageIO.write(bImage, "jpeg", oFile);//保存图像文件
}catch(IOException ex)
{
ex.printStackTrace();
}
}
}
但是这样保存后 生成的图片是整个panel的大小 ,不是我想要的结果,我想要只保存panel中框的大小 (框的x,y,width,height都知道)不想截图 ,怎么完成


------解决方案--------------------

BufferedImage  getSubimage(int x, int y, int w, int h)

Graphics2D g2 =  image.createGraphics(); 
Main.mp.paint(g2); 
save(image.getSubimage(x,y,w,h));