Results 1 to 1 of 1
  1. #1
    wolfgang76 is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default How to save a canvas in a scrolled composite as image

    Hello,

    I tried to save the content of a SWT-Canvas which is included in a scrolled composite as image to my harddrive. The following code doesn't work, because it doesn't make an image from the whole canvas, only from the visible part. It also includes outer parts. I also attached the sources and a screenshot. Do you know how to fix this problem?

    Thank you in advance !!

    Wolfgang


    GUIShellDrawing.java
    Java Code:
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.events.SelectionAdapter;
    import org.eclipse.swt.events.SelectionEvent;
    import org.eclipse.swt.layout.GridLayout;
    import org.eclipse.swt.layout.GridData;
    
    public class GUIShellDrawing extends Shell {
    
    	private static GUIMyComposite mycomposite = null;
    
    	/**
    	 * Launch the application.
    	 * 
    	 * @param args
    	 */
    	public static void main(String args[]) {
    		try {
    			Display display = Display.getDefault();
    			GUIShellDrawing shell = new GUIShellDrawing(display);
    			shell.open();
    			shell.layout();
    			mycomposite.myredrawit();
    			while (!shell.isDisposed()) {
    				if (!display.readAndDispatch()) {
    					display.sleep();
    				}
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	/**
    	 * Create the shell.
    	 * 
    	 * @param display
    	 */
    	public GUIShellDrawing(Display display) {
    		super(display, SWT.SHELL_TRIM);
    		createContents();
    	}
    
    	/**
    	 * Create contents of the shell.
    	 */
    	protected void createContents() {
    		setText("SWT Application");
    		setSize(508, 386);
    		setLayout(new GridLayout(1, false));
    		Button btnMakePicture = new Button(this, SWT.NONE);
    		btnMakePicture.addSelectionListener(new SelectionAdapter() {
    			public void widgetSelected(SelectionEvent e) {
    				mycomposite.writeContentToFile("d:/test.bmp");
    			}
    		});
    		btnMakePicture.setText("Make Picture...");
    
    		mycomposite = new GUIMyComposite(this, SWT.BORDER);
    		mycomposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true, 1, 1));
    	}
    
    	@Override
    	protected void checkSubclass() {
    		// Disable the check that prevents subclassing of SWT components
    	}
    }


    GUIMyComposite.java
    Java Code:
    import org.eclipse.swt.SWT;
    
    public class GUIMyComposite extends Composite {
    
    	private ScrolledComposite myouterscroll;
    	private Canvas mycanvas;
    
    	public GUIMyComposite(Composite parent, int style) {
    		super(parent, style);
    		setLayout(new GridLayout(1, false));
    		
    		Label lblALittleCaption = new Label(this, SWT.NONE);
    		lblALittleCaption.setText("A little caption...");
    		myouterscroll = new ScrolledComposite(this, SWT.BORDER
    				| SWT.H_SCROLL | SWT.V_SCROLL);
    		myouterscroll.setAlwaysShowScrollBars(true);
    		myouterscroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
    				true, 2, 1));
    		myouterscroll.setExpandHorizontal(true);
    		myouterscroll.setExpandVertical(true);
    		mycanvas = new Canvas(myouterscroll, SWT.NONE);		
    		mycanvas.setLayout(null);
    		mycanvas.setSize(4000,4000);
    		
    		Label lbltopleft = new Label(mycanvas, SWT.NONE);
    		lbltopleft.setBounds(10, 10, 55, 15);
    		lbltopleft.setText("(topleft)");
    		
    		Label lblbottomright = new Label(mycanvas, SWT.NONE);
    		lblbottomright.setBounds(3900, 3900, 55, 15);
    		lblbottomright.setText("(bottomright)");
    		
    		Button button = new Button(mycanvas, SWT.NONE);
    		button.setBounds(183, 400, 75, 25);
    		button.setText("New Button");
    		myouterscroll.setContent(mycanvas);
    		mycanvas.addPaintListener(new PaintListener() {
    			public void paintControl(PaintEvent e) {
    				zeichneDiagramm(e.gc, e.display);
    			}
    		});
    		myredrawit();
    	}
    
    	protected void zeichneDiagramm(GC gc, Display display) {
    		// Durchführen von Zeichenoperationen (> als sichtbarer Bereich...)
    		gc.drawLine(0, 0, 4000, 4000);
    		gc.drawLine(0, 4000, 4000, 0);
    	}
    
    	public void writeContentToFile(String filename) {
    		GC gc = new GC(mycanvas);
    		Image image = new Image(this.getDisplay(), mycanvas.getSize().x,
    				mycanvas.getSize().y);
    		zeichneDiagramm(gc, this.getDisplay());
    		gc.copyArea(image, 0, 0);
    		ImageLoader loader = new ImageLoader();
    		loader.data = new ImageData[] { image.getImageData() };
    		loader.save(filename, SWT.IMAGE_BMP);
    		gc.dispose();
    	}
    
    	public void myredrawit() {
    		redraw();
    		myouterscroll.setMinSize(mycanvas.computeSize(SWT.DEFAULT,
    				SWT.DEFAULT));
    	}
    }
    Attached Files Attached Files

Similar Threads

  1. how to save image as jpg?
    By katie in forum New To Java
    Replies: 4
    Last Post: 04-15-2010, 10:07 AM
  2. Replies: 0
    Last Post: 07-05-2009, 12:31 PM
  3. how can i save an image from url
    By happyday in forum Advanced Java
    Replies: 2
    Last Post: 01-14-2009, 07:02 PM
  4. Canvas Image popups another image (SWT)
    By SpaceY in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 01:25 PM
  5. Visualizing an image on Canvas
    By a_klasanov in forum CLDC and MIDP
    Replies: 2
    Last Post: 08-01-2008, 10:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •