Results 1 to 1 of 1
- 12-30-2010, 01:32 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 1
- Rep Power
- 0
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)); } }
Similar Threads
-
how to save image as jpg?
By katie in forum New To JavaReplies: 4Last Post: 04-15-2010, 10:07 AM -
SWT Canvas drawing realtive to image space not canvas space.
By bobbie in forum SWT / JFaceReplies: 0Last Post: 07-05-2009, 12:31 PM -
how can i save an image from url
By happyday in forum Advanced JavaReplies: 2Last Post: 01-14-2009, 07:02 PM -
Canvas Image popups another image (SWT)
By SpaceY in forum New To JavaReplies: 2Last Post: 11-11-2008, 01:25 PM -
Visualizing an image on Canvas
By a_klasanov in forum CLDC and MIDPReplies: 2Last Post: 08-01-2008, 10:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks