Resizing an Image: Without AntiAliasing it doesnt look good [message #1015759] |
Sat, 02 March 2013 10:27 |
Simon Mising name Messages: 12 Registered: December 2011 |
Junior Member |
|
|
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 476 times)
|
|
|
|
Re: Resizing an Image: Without AntiAliasing it doesnt look good [message #1053263 is a reply to message #1015759] |
Fri, 03 May 2013 12:53 |
Vijay Raj Messages: 608 Registered: July 2009 |
Senior Member |
|
|
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.
---------------------
why, mr. Anderson, why, why do you persist?
Because I Choose To.
Regards,
Vijay
|
|
|
|
Powered by
FUDForum. Page generated in 0.04303 seconds