将完整滚动的合成导出到图像

时间:2013-02-10 07:33:15

标签: java image swt scrolledcomposite

我想将完整滚动的合成导出到图像。我指的是整个复合材料,而不仅仅是复合材料的可见区域。 我找到了这个例子:

        Image drawable = new Image(display, someComposite.getBounds());
        GC gc = new GC(drawable);
        mainWidget.print(gc);
        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] {drawable.getImageData()};
        loader.save("exportedImage.bmp", SWT.IMAGE_BMP);
        drawable.dispose();
        gc.dispose(); 

但输出是滚动复合的可见区域。 :(而不是全部,我想这是因为.getBound()方法,但我无法找到一种方法将其切换到另一种方法。我试过.getClientArea()但它没有用...

提前致谢, 贝纳。

1 个答案:

答案 0 :(得分:2)

我找到了一个例子并修改了一些东西 - 只需复制和放大粘贴它:

import org.eclipse.swt.*;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;


public class ScroledCompositeExport {
    public static void main (String [] args) {
        final Display display = new Display ();
     Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setSize(200, 200);
        shell.setLocation(20, 20);

        final ScrolledComposite sc = new ScrolledComposite(shell, SWT.BORDER  | SWT.V_SCROLL | SWT.H_SCROLL);
        sc.setLayout(new GridLayout(1,true));

        final Composite innerComposite = new Composite(sc, SWT.NONE);
        innerComposite.setSize(400, 400);
        innerComposite.setLayout(new GridLayout(1, true));
        innerComposite.setBackground(new Color(display, 255, 0, 0));

        for(int i = 0; i < 10; i++)
        {
                Button b = new Button(innerComposite, SWT.PUSH);
                b.setText("Text");
                b.addSelectionListener(new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent e) 
                    {
                        Image image = new Image(display, innerComposite.getBounds().width, innerComposite.getBounds().height);
                        ImageLoader loader = new ImageLoader();

                        GC gc = new GC(image);
                        innerComposite.print(gc);

                        gc.dispose();

                        loader.data = new ImageData[]{image.getImageData()};
                        loader.save("c:/temp/out.png", SWT.IMAGE_PNG);
                    }
                });
        }

        sc.setMinSize(innerComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
        sc.setContent(innerComposite);
        sc.setExpandHorizontal(true);
        sc.setExpandVertical(true);

        shell.open();
        shell.open ();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }
        display.dispose ();
    }
}

它正在工作:) 并且可以从一开始就很好。