How do I add constraints to image resizing code? I want the image to be no larger then 165×146. The below code does not hold the constraint when image is 525×610
intWidth = 165 '*** Fix Width ***'
intHeight = 146 '*** Fix Width ***'
If objGraphic.Width > intWidth Then
Dim ratio As Double = objGraphic.Height / objGraphic.Width
intHeight = ratio * intWidth
objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
ElseIf objGraphic.Height > intHeight Then
Dim ratio As Double = objGraphic.Width / objGraphic.Height
intWidth = ratio * intHeight
objBitmap = New Bitmap(objGraphic, intWidth, intHeight)
Else
objBitmap = New Bitmap(objGraphic)
End If
I think you want to maintain the aspect ratio of your image? If so, this method might be appropriate; you will need to multiply the width and height by the ratio you obtain.
Edit: Probably need to explicitly convert new height and width to ints