3

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

        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()但没有奏效......

在此先感谢,约西。

4

1 回答 1

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 ();
    }
}

它正在工作:) 并且可以作为开始的好点。

于 2013-02-10T09:16:51.993 回答