Code to scale JFace/SWT image
Please find the code below
ImageDescriptor scaleImage(Display display, ImageDescriptor imageDescriptor,
int outMaxWidth, int outMaxHeight) {
if (imageDescriptor == null) {
return null;
}
ImageData imageData = imageDescriptor.getImageData();
if (imageData == null) {
return imageDescriptor;
}
int newHeight = outMaxHeight;
int newWidth = (imageData.width * newHeight) / imageData.height;
if (newWidth > outMaxWidth) {
newWidth = outMaxWidth;
newHeight = (imageData.height * newWidth) / imageData.width;
}
// Use GC.drawImage
Image outImage = new Image(display, newWidth, newHeight);
GC gc = new GC(outImage);
Image oldImage = imageDescriptor.createImage();
gc.drawImage(oldImage, 0, 0, imageData.width, imageData.height, 0, 0, newWidth, newHeight);
ImageDescriptor outImageDesc = ImageDescriptor.createFromImage(outImage);
oldImage.dispose();
gc.dispose();
return outImageDesc;
}
Comments
Post a Comment