Code to proportionately resize an iOSImage

My app needs to show images at various sizes (e.g. thumbnail in an iOSTable, larger in a view) and I’ve been using DrawImage and passing the screen scale factor to get the appropriate density of image based on whether the device has a retina display or not.

The problem I have is that DrawImage does not draw the image proportionately; so a portrait image is stretched to fill a landscape space, etc.

Does anybody have any proportionate image resizing code that they’d be willing to share?

[quote=183349:@Jason Tait]My app needs to show images at various sizes (e.g. thumbnail in an iOSTable, larger in a view) and I’ve been using DrawImage and passing the screen scale factor to get the appropriate density of image based on whether the device has a retina display or not.

The problem I have is that DrawImage does not draw the image proportionately; so a portrait image is stretched to fill a landscape space, etc.

Does anybody have any proportionate image resizing code that they’d be willing to share?[/quote]

You can probably adapt the ProportionalScale method from here :
https://forum.xojo.com/17230-is-there-a-simple-way-such-as-property-method-to-scale-image-in

Does the ScaleImage method I posted here (and added to iOSKit) not do it?
https://forum.xojo.com/21461-image-resizing-techniques

[quote=183360:@Jason King]Does the ScaleImage method I posted here (and added to iOSKit) not do it?
https://forum.xojo.com/21461-image-resizing-techniques[/quote]

That function works really well for scaling an image and I use it in my app. But what I need to do is simulate what an iOSImageView (I guess it’s really an NSImageWell) does with a mix of portrait and landscape images to display them all at the same size. Actually you can see this in the Photos app on your iPhone as well. If you take a mix of portrait and landscape images, all the images appear to be the same size in the Photos thumbnail view. The iOSImageView seems to stretch portrait images so they are outside the bounds of the thumbnail dimensions and then crop them. The result is that all images remain in proportion and yet fill the entirety of the thumbnail dimensions.

You should be able to adapt the follow code…

Private Sub DrawPictureInto(g As Graphics, p As Picture, KeepProportions As Boolean = True, ResizeSmaller As Boolean = False)
  //   KeepProportions: If True ( Default ), the picture's aspect ratio wil be respected.
  //                    If False, the picture will be redimensioned to fit the Graphics object entirely
  //
  //
  //   ResizeSmaller: If True, the smaller pictures wil be resized to fit the Graphics object dimensions.
  //                  If False ( default ), the smaller pictures will just be drawn centered
  
  If p Is Nil then Return
  
  If KeepProportions then
    // No needs to keep the proportions, so just fill g entirely with the p.
    g.DrawPicture p, 0, 0, g.Width, g.Height, 0, 0, p.Width, p.Height
  Else
    If p.Width <= g.Width AND p.Height <= g.Height AND Not ResizeSmaller then
      // The picture is smaller than g size, so we just need to draw it centered.
      g.DrawPicture p, (g.width - p.Width)/2, (g.Height - p.Height)/2
    Else
      Dim XScale, YScale, ScaleFactor As Double
      Dim NewWidth, NewHeight As Integer
      
      // At least one of the picture's dimensions is bigger than g size
      // Choose the smallest scaling factor.
      XScale = g.Width / p.Width
      YScale = g.Height / p.Height
      ScaleFactor = min(XScale, YScale)
      
      NewWidth  = Round( p.Width*ScaleFactor )
      NewHeight = Round( p.Height*ScaleFactor )
      
      g.DrawPicture p, ( g.width - NewWidth ) / 2, ( g.Height - NewHeight ) / 2, NewWidth, NewHeight, 0, 0, p.Width, p.Height
    End If
  End If
  
End Sub

Thank you @Matthew Combatti . That is working well.

You’re welcome :slight_smile: