[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.gef] Re: Export to png or jpeg

Antonio,

Here is some code for exporting a GEF figure to an image.
Regards,

Remko Popma

/**
 * Returns the bytes of an encoded image for the specified
 * IFigure in the specified format.
 *
 * @param figure the Figure to create an image for.
 * @param format one of SWT.IMAGE_BMP, SWT.IMAGE_BMP_RLE, SWT.IMAGE_GIF
 *          SWT.IMAGE_ICO, SWT.IMAGE_JPEG, or SWT.IMAGE_PNG
 * @return the bytes of an encoded image for the specified Figure
 */
private byte[] createImage(IFigure figure, int format) {

    Device device = getEditPartViewer().getControl().getDisplay();
    Rectangle r = figure.getBounds();

    ByteArrayOutputStream result = new ByteArrayOutputStream();

    Image image = null;
    GC gc = null;
    Graphics g = null;
    try {
        image = new Image(device, 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();
}



Antonio Muñoz wrote:
Does anybody know how to export a gef diagram / image to a png image (or
jpeg) ?

I tryed to export it to svg directly to, afterwards, transform it to a
jpeg image. But I didn't found a way to do it.

Thanx