如何向图像调整大小代码添加约束?即不大于165x146
如何向图像调整大小代码添加约束?我希望图像不大于 165x146。当图像为 525x610 时,以下代码不保留约束
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
How do I add constraints to image resizing code? I want the image to be no larger then 165x146. The below code does not hold the constraint when image is 525x610
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您想保持图像的纵横比?如果是这样,这个方法可能是合适的;您需要将宽度和高度乘以您获得的比率。
编辑:可能需要将新的高度和宽度显式转换为整数
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