[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform.swt] Re: Displaying Image Thumbnails

Can you try this code out and let me know how you get on?  I'm considering
add it as a snippet.
From your post, I can't tell whether you can't scale an image at all, it is
too slow or you keep getting
the out of memory error.  This code just scales the images.

/*
 * Copyright (c) 2000, 2003 IBM Corp.  All rights reserved.
 * This file is made available under the terms of the Common Public License
v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.custom.*;

import java.io.*;

public class Thumbnail {
public static void main (String [] args) {
 final Display display = new Display ();
 final Shell shell = new Shell (display);
 Button button = new Button (shell, SWT.PUSH);
 button.setText ("Load Images ...");
 final ScrolledComposite sc = new ScrolledComposite (shell, SWT.H_SCROLL |
SWT.V_SCROLL);
 final Composite comp = new Composite (sc, SWT.NONE);
 comp.setLayout (new RowLayout (SWT.VERTICAL));
 sc.setContent (comp);
 FormLayout layout = new FormLayout ();
 FormData scData = new FormData ();
 scData.left = new FormAttachment (0, 0);
 scData.right = new FormAttachment (100, 0);
 scData.top = new FormAttachment (button, 0);
 scData.bottom = new FormAttachment (100, 0);
 sc.setLayoutData (scData);
 shell.setLayout (layout);
 final String [] path = new String [] {"j:/teamswt/wadman/doc/jdj/part2"};
 button.addListener (SWT.Selection, new Listener () {
  public void handleEvent (Event e) {
   DirectoryDialog dialog = new DirectoryDialog (shell);
   dialog.setFilterPath (path [0]);
   if (dialog.open () == null) return;
   Control [] children = comp.getChildren ();
   for (int i=0; i<children.length; i++) {
    children [i].dispose ();
   }
   File file = new File (path [0]);
   File [] files = file.listFiles ();
   for (int i= 0; i<files.length; i++) {
    if (!files [i].isDirectory()) {
     final Image image;
     try {
      image = new Image (display, files [i].getAbsolutePath ());
     } catch (SWTException exception) {
      continue;
     }
     final Composite c = new Composite (comp, SWT.BORDER);
     Listener listener = new Listener () {
      public void handleEvent (Event e) {
       switch (e.type) {
        case SWT.Dispose: image.dispose (); break;
        case SWT.Paint: {
         Rectangle rect = c.getClientArea ();
         Rectangle bounds = image.getBounds ();
         int x = 0, y = 0, width = 0, height = 0;
         if (bounds.width > bounds.height) {
          width = rect.width;
          height = bounds.height * rect.height / bounds.width;
          y = (rect.height - height) / 2;
         } else {
          height = rect.height;
          width = bounds.width * rect.width / bounds.height;
          x = (rect.width - width) / 2;
         }
         e.gc.drawImage (image, 0, 0, bounds.width, bounds.height, x, y,
width, height);
        }
       }
      }
     };
     c.addListener (SWT.Dispose, listener);
     c.addListener (SWT.Paint, listener);
     c.setLayoutData (new RowData (100, 100));
    }
   }
   comp.pack ();
   sc.layout ();
  }
 });
 shell.setSize (300, 300);
 shell.open ();
 while (!shell.isDisposed ()) {
  if (!display.readAndDispatch ()) display.sleep ();
 }
 display.dispose ();
}
}

"Schnul" <schnul@xxxxxx> wrote in message news:b7p0vh$pd$1@xxxxxxxxxxxxxxxx
> Hi there!
>
> I hope this is the right place to post this question... I am working on a
> standalone application and started using SWT as I think it's a really
great
> UI.
>
> Now I would like to display a thumbnail, but I don't know whether or not
> there is a way to do that and if yes, how to do it. My images are quite
big
> and would like to avoid that I've to create own thumbnail files. When I
try
> to use the ImageDescriptor I already get an Out of Memory error (I tried
to
> use the descriptor and then to resize the image).
>
> I would really be happy if somebody could let me know if there is a way to
> do that. Might be there is no way yet, because SWT was not designed for
such
> reasons (or not yet).
>
> Thanks,
> Chris
>
>