Resize a bitmap
This function resizes a bitmap to fit given dimensions without stretching the bitmap
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Public Module Bitmap
Public Function Resize(ByVal Bitmap As System.Drawing.Bitmap, ByVal MaxWidth As Integer, ByVal MaxHeight As Integer) As System.Drawing.Bitmap
If Bitmap Is Nothing Then Throw New Exception("No bitmap supplied for resize")
If MaxWidth <= 0 Then Throw New Exception("Invalid MaxWidth supplied for resizing bitmap")
If MaxHeight <= 0 Then Throw New Exception("Invalid MaxHeight supplied for resizing bitmap")
Dim TargetWidth, TargetHeight As Integer
TargetWidth = Bitmap.Width
TargetHeight = Bitmap.Height
If Bitmap.Width > MaxWidth Then
TargetWidth = MaxWidth
TargetHeight = Bitmap.Height * MaxWidth / Bitmap.Width
If TargetHeight > MaxHeight Then
TargetHeight = MaxHeight
TargetWidth = TargetWidth * MaxHeight / TargetHeight
End If
End If
If Bitmap.Height > MaxHeight Then
TargetHeight = MaxHeight
TargetWidth = Bitmap.Width * MaxHeight / Bitmap.Height
If TargetWidth > MaxWidth Then
TargetWidth = MaxWidth
TargetHeight = TargetHeight * MaxWidth / TargetWidth
End If
End If
Return New System.Drawing.Bitmap(Bitmap, TargetWidth, TargetHeight)
End Function
End Module