Java SWT Image Resize 正常工作
ImageLoader 类用于从文件或流中加载图像并将图像保存到文件或流中
imageLoader.save(result, SWT.IMAGE_COPY)
FileDialog 类允许用户浏览文件系统并选择或输入文件名。
Button btnOpen = new Button(parent, SWT.NONE);
btnOpen.setBounds(200, 55, 68, 23);
btnOpen.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
String result = dialog.open();
if(result!=null)
{
Image image=SWTResourceManager.getImage(result);
//ImageData class are device-independent descriptions of images
ImageData imgData = image.getImageData();
imgData=imgData.scaledTo(200, 200);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] {imgData};
imageLoader.save(result, SWT.IMAGE_COPY);
System.out.println("Width: "+imgData.width+".....Height: "+imgData.height);
lbl_image_text.setBounds(25,88,imgData.width+10,imgData.height+10);
lbl_image_text.setImage(SWTResourceManager.getImage(result));
}
}
});
btnOpen.setText("open");
CLabel lbl_image_text = new CLabel(parent, SWT.Resize);
图像大小设置为动态标签
Button btnOpen = new Button(parent, SWT.NONE);
btnOpen.setBounds(200, 55, 68, 23);
btnOpen.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN);
String result = dialog.open();
if(result!=null)
{
Image image=SWTResourceManager.getImage(result);
//get Image width and height
lbl_image_text.setBounds(25,88,image.getBounds().width+10,image.getBounds().height+10);
lbl_image_text.setImage(SWTResourceManager.getImage(result));
}
}
});
btnOpen.setText("open");
CLabel lbl_image_text = new CLabel(parent, SWT.Resize);