private byte[] createImage(IFigure figure, int format) {
Rectangle r = figure.getBounds();
ByteArrayOutputStream result = new ByteArrayOutputStream();
Image image = null;
GC gc = null;
Graphics g = null;
try {
image = new Image(null, r.width, r.height);
gc = new GC(image);
g = new SWTGraphics(gc);
g.translate(r.x * -1, r.y * -1);
figure.paint(g);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save(result, format);
} finally {
if (g != null) {
g.dispose();
}
if (gc != null) {
gc.dispose();
}
if (image != null) {
image.dispose();
}
}
return result.toByteArray();
}
byte[] data = createImage(figure, SWT.IMAGE_PNG);
try {
FileOutputStream fos = new FileOutputStream(filename);
fos.write(data);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
从这里摘的,比较有用:http://www.cnblogs.com/bjzhanghao/archive/2007/08/01/838278.html
本文介绍了一种使用SWT(Graphics Widget Toolkit)将IFigure对象绘制并保存为PNG图片的方法。通过创建图像、利用SWTGraphics进行绘制,并最终转换为字节数组保存到文件中。

8220

被折叠的 条评论
为什么被折叠?



