swt - How display a resized image with aspect ratio preserved in a fixed size Label in eclipse e4 rcp? -
i want display preview image resized version of original image aspect ratio preserved in fixed size label. example, have image of 1024x786 , want display in label has size 500x500. want fit in label keeping aspect ratio of image intact.
it nice if image automatically resized when user resizes part.
is possible label? or need canvas this?
this image scaling code based on jface imagedescriptor:
imagedescriptor scaleimage(display display, imagedescriptor imagedesc, int maxwidth, int maxheight) { if (imagedesc == null) return null; imagedata imagedata = imagedesc.getimagedata(); if (imagedata == null) // can jpeg using cmyk colour space etc. return imagedesc; int newheight = maxheight; int newwidth = (imagedata.width * newheight) / imagedata.height; if (newwidth > maxwidth) { newwidth = maxwidth; newheight = (imagedata.height * newwidth) / imagedata.width; } // use gc.drawimage scale gives better result on mac image newimage = new image(display, newwidth, newheight); gc gc = new gc(newimage); image oldimage = imagedesc.createimage(); gc.drawimage(oldimage, 0, 0, imagedata.width, imagedata.height, 0, 0, newwidth, newheight); imagedescriptor result = imagedescriptor.createfromimage(newimage); oldimage.dispose(); gc.dispose(); return result; }
Comments
Post a Comment