Public Function ScaleImageToFit(ByVal sourceImg As String, ByVal pbWidth As Integer, ByVal pbHeight As Integer) As BitmapImage Dim sourceImage As New BitmapImage() sourceImage.BeginInit() sourceImage.UriSource = New Uri(sourceImg, UriKind.Relative) sourceImage.EndInit() Dim scale_factor As Single = 0 ' determine scale_factor based on source image size. Dim srcWidth As Integer = sourceImage.Width Dim srcHeight As Integer = sourceImage.Height Dim myImage As New Image If srcWidth > srcHeight Then ' landscape scale_factor = pbWidth / srcWidth myImage.Width = pbWidth * scale_factor sourceImage.DecodePixelWidth = myImage.Width Else ' portrait scale_factor = pbHeight / srcHeight ' make it fit to pb height myImage.Height = pbWidth * scale_factor sourceImage.DecodePixelHeight = myImage.Height End If myImage.Source = sourceImage Return myImage.Source End Function
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)