public static Image createImage(GraphicalEditPart part) {
IFigure figure = part.getFigure();
Rectangle figureBounds = figure.getBounds();
Control figureCanvas = part.getViewer().getControl();
double scale = ((ScalableRootEditPart) part.getViewer().getRootEditPart()).getZoomManager().getZoom();
// calculate feedback maximum dimension
Dimension dimension = new Dimension((int) Math.round(figureBounds.width*scale),
(int) Math.round(figureBounds.height*scale));
Image img = new Image(null, dimension.width, dimension.height);
img.setBackground(ColorConstants.white);
GC imageGC = new GC(img);
Graphics imgGraphics = new SWTGraphics(imageGC);
imgGraphics.scale(scale);
imgGraphics.translate(-figureBounds.x, -figureBounds.y);
figure.paint(imgGraphics);
ImageData data = img.getImageData();
Image feedbackImage = new Image(figureCanvas.getDisplay(), data);
imageGC.dispose();
img.dispose();
return feedbackImage;
}
you can save it using this one:
public static void saveImageFile(File file, Image image) throws IOException, FileNotFoundException {
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] {image.getImageData()};
OutputStream os = new FileOutputStream(file);
loader.save(os, SWT.IMAGE_PNG);
os.close();
}
Regards,
Enrico Persiani