Eclipse Search

Loading

Mar 18, 2008

Exporting GEF figure to an image

Quite often you need to export the current contents of a GEF editor to an image. Here is the simple code to do that:

GraphicalViewer graphicalViewer = ...; // get it from your editor
String saveLocation = ...; // get it thru a FileDialog

ScalableFreeformRootEditPart rootEditPart = (ScalableFreeformRootEditPart) graphicalViewer.getEditPartRegistry().get(LayerManager.ID);

IFigure rootFigure = ((LayerManager) rootEditPart).getLayer(LayerConstants.PRINTABLE_LAYERS);

Rectangle rootFigureBounds = rootFigure.getBounds();

Control figureCanvas = graphicalViewer.getControl();


Image img = new Image(Display.getDefault(), rootFigureBounds.width, rootFigureBounds.height);

GC imageGC = new GC(img);

figureCanvas.print(imageGC); // This is Eclipse 3.4 only API

ImageLoader imgLoader = new ImageLoader();
imgLoader.data = new ImageData[] { img.getImageData() };

imgLoader.save(saveLocation, SWT.IMAGE_JPEG);

imageGC.dispose();

img.dispose();



Just in case you need, here is a nice image to represent this Action :-)

0 comments:

Post a Comment