Skip to main content



      Home
Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Resizing an Image: Without AntiAliasing it doesnt look good
Resizing an Image: Without AntiAliasing it doesnt look good [message #1015759] Sat, 02 March 2013 05:27 Go to next message
Eclipse UserFriend
Hi,
Ive wrote a simple SWT demo app to dynamically resize an image. It works quite fine.
But the problem is:
If I draw the image (Canvas) smaller than it actually is, it doesnt look good any more (see attachment). It doesnt matter if I activate AntiAliasing within the canvas or not. It always does not look quite well.

Is there any trick (without using Swing instead) to get a cleaner ("AntiAliased") image?

Thanks for you help

Greetings from Germany
Simon

Here a short demo app to demonstrate the effekt:

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {

	protected Shell shell;
	private Canvas canvas;
	private Image imgInput;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			Test window = new Test();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
		shell.setLayout(new FillLayout());
		canvas = new Canvas(shell, SWT.NO_BACKGROUND);
		File input = new File("C:\\Users\\Simon\\Desktop\\atemschutz.png");

		imgInput = new Image(Display.getDefault(), input.getAbsolutePath());

		canvas.addPaintListener(new ImagePaintListener());

	}

	private class ImagePaintListener implements PaintListener {

		@Override
		public void paintControl(PaintEvent e) {
			// Calculate positions

			int width = e.width;
			int height = e.height;
			// Create the image to fill the canvas
			Image image = new Image(Display.getCurrent(), canvas.getBounds());

			// Set up the offscreen gc
			GC gcImage = new GC(image);
			// Draw the background
			gcImage.fillRectangle(image.getBounds());
			gcImage.setAntialias(SWT.ON);
			gcImage.setAdvanced(true);
			e.gc.setAntialias(SWT.ON);
			if (imgInput != null) {
				// Noch keine Daten >> Logo
				// 260x113
				float scale = 1.0f;
				ImageData data = imgInput.getImageData();
				if (width < data.width) {
					scale = width / (float) data.width;
				}
				if (height < data.height) {
					float tmpScale = height / (float) data.height;
					scale = scale < tmpScale ? scale : tmpScale;
				}
				if (scale > 0.0) {
					Image scaledImage = new Image(e.display, data.scaledTo(Math.round(data.width * scale), Math.round(data.height * scale)));
					int imgWidth = scaledImage.getImageData().width;
					int imgHeight = scaledImage.getImageData().height;
					// Bild in voller Größe
					gcImage.drawImage(scaledImage, width / 2 - imgWidth / 2, height / 2 - imgHeight / 2);
					scaledImage.dispose();
				}
			}

			// Draw the offscreen buffer to the screen
			e.gc.drawImage(image, 0, 0);

			// Clean up
			image.dispose();
			gcImage.dispose();
			e.gc.dispose();
		}
	}

}

  • Attachment: example.png
    (Size: 318.76KB, Downloaded 519 times)
Re: Resizing an Image: Without AntiAliasing it doesnt look good [message #1049767 is a reply to message #1015759] Fri, 26 April 2013 04:50 Go to previous messageGo to next message
Eclipse UserFriend
sorry, i am not new to this
Re: Resizing an Image: Without AntiAliasing it doesnt look good [message #1053263 is a reply to message #1015759] Fri, 03 May 2013 08:53 Go to previous messageGo to next message
Eclipse UserFriend
why not just use the method
gc.drawImage(Image image, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight)


to scale the image.
icon14.gif  Re: Resizing an Image: Without AntiAliasing it doesnt look good [message #1053383 is a reply to message #1053263] Sat, 04 May 2013 14:14 Go to previous message
Eclipse UserFriend
Thanks!
The result of this method looks much better then my solution!

Thread can be closed...
Previous Topic:Table Cell and two progress bars
Next Topic:Widget is disposed (newb edition)
Goto Forum:
  


Current Time: Wed Jul 23 12:23:07 EDT 2025

Powered by FUDForum. Page generated in 0.08986 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top