Save root figure as SWT Image [message #189868] |
Sat, 30 July 2005 11:29 |
Eclipse User |
|
|
|
Originally posted by: venkataraman.m.tcs.com
Hi, Is there any ready to use API with GEF to save a Draw2D figure as an
SWT Image. I digged into Draw2D and GEF, but could not figure out a simple
way of achieving that. If there is any simple way, could you please
suggest the same. I have arrived at some way doing that by the following
code..if there are any issues with this, please post.
-----------------
public void saveEditorContentsAsImage(GraphicalViewer viewer, String
saveFilePath, int format)
{
/* 1. First get the figure whose visuals we want to save as image.
* So we would like to save the rooteditpart which actually hosts all the
printable layers.
*
* NOTE: ScalableRootEditPart manages layers and is registered
graphicalviewer's editpartregistry with
* the key LayerManager.ID ... well that is because ScalableRootEditPart
manages all layers that
* are hosted on a FigureCanvas. Many layers exist for doing different
things */
ScalableRootEditPart rootEditPart =
(ScalableRootEditPart)viewer.getEditPartRegistry().get(Layer Manager.ID);
IFigure rootFigure =
((LayerManager)rootEditPart).getLayer(LayerConstants.PRINTAB LE_LAYERS);//rootEditPart.getFigure();
Rectangle rootFigureBounds = rootFigure.getBounds();
/* 2. Now we want to get the GC associated with the control on which all
figures are
* painted by SWTGraphics. For that first get the SWT Control associated
with the viewer on which the
* rooteditpart is set as contents */
Control figureCanvas = viewer.getControl();
GC figureCanvasGC = new GC(figureCanvas);
/* 3. Create a new Graphics for an Image onto which we want to paint
rootFigure */
Image img = new Image(null, rootFigureBounds.width,
rootFigureBounds.height);
GC imageGC = new GC(img);
imageGC.setBackground(figureCanvasGC.getBackground());
imageGC.setForeground(figureCanvasGC.getForeground());
imageGC.setFont(figureCanvasGC.getFont());
imageGC.setLineStyle(figureCanvasGC.getLineStyle());
imageGC.setLineWidth(figureCanvasGC.getLineWidth());
imageGC.setXORMode(figureCanvasGC.getXORMode());
Graphics imgGraphics = new SWTGraphics(imageGC);
/* 4. Draw rootFigure onto image. After that image will be ready for save
*/
rootFigure.paint(imgGraphics);
/* 5. Save image */
ImageData[] imgData = new ImageData[1];
imgData[0] = img.getImageData();
ImageLoader imgLoader = new ImageLoader();
imgLoader.data = imgData;
imgLoader.save(saveFilePath, format);
/* release OS resources */
figureCanvasGC.dispose();
imageGC.dispose();
img.dispose();
}
-------------------------
Thanks
Venkat
|
|
|
Powered by
FUDForum. Page generated in 0.03205 seconds