title: SWT Composit/Canvas Fit Image
body: <p>I am trying to fit an image to a composite (or a canvas) which can be resized with the window. When it does, I want to be sure it doesn't mess up the aspect ratio, so I wrote this code to override the print function:</p>
  <pre><code>      bazaarMenu.addPaintListener(new PaintListener() {
              public void paintControl(PaintEvent event) {
                  Image image = SWTResourceManager.getImage(mainFrame.class, &quot;/assets/SkyHub/resources/images/bazaarBackground.jpg&quot;);//get the image
                  int w = bazaarMenu.getClientArea().width;//composite width
                  int h = bazaarMenu.getClientArea().height;//composite height
                  int iw = image.getBounds().width;//image width
                  int ih = image.getBounds().height;// image height
                  
                  //these are the values of the width/height of the image if ratio is same with the 
                  //composite's
                  int w1 = ih * (w / h);
                  int h1 = iw * (h / w);
                  
                  //Find which value can be used and draw the image
                  if(w1 &lt;= w)
                  {
                      event.gc.drawImage(image, 0, 0, w1, ih, 0, 0, w, h);
                  }
                  else// if(w / h &lt; 76 / 45)
                  {
                      event.gc.drawImage(image, 0, 0, iw, h1, 0, 0, w, h);
                  }
              }
         });
  </code></pre>
  <p>However, I cannot get the result I want. Either image streches or only a part of it can be seen. Is there any other way to do or maybe I'm missing something.</p>