如何向图像调整大小代码添加约束?即不大于165x146

发布于 2025-01-06 03:21:32 字数 760 浏览 2 评论 0原文

如何向图像调整大小代码添加约束?我希望图像不大于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江心雾 2025-01-13 03:21:32

我认为您想保持图像的纵横比?如果是这样,这个方法可能是合适的;您需要将宽度和高度乘以您获得的比率。

'define max size of image
intWidth = 165
intHeight = 146

'if the width/height is larger than the max, determine the appropriate ratio
Dim widthRatio as Double = Math.Min(1, intWidth / objGraphic.Width)
Dim heightRatio as Double = Math.Min(1, intHeight / objGraphic.Height)

'take the smaller of the two ratios as the one we will use to resize the image
Dim ratio as Double = Math.Min(widthRatio, heightRatio)

'apply the ratio to both the width and the height to ensure the aspect is maintained
objBitmap = New Bitmap(objGraphic, CInt(objGraphic.Width * ratio), CInt(objGraphic.Height * ratio))

编辑:可能需要将新的高度和宽度显式转换为整数

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.

'define max size of image
intWidth = 165
intHeight = 146

'if the width/height is larger than the max, determine the appropriate ratio
Dim widthRatio as Double = Math.Min(1, intWidth / objGraphic.Width)
Dim heightRatio as Double = Math.Min(1, intHeight / objGraphic.Height)

'take the smaller of the two ratios as the one we will use to resize the image
Dim ratio as Double = Math.Min(widthRatio, heightRatio)

'apply the ratio to both the width and the height to ensure the aspect is maintained
objBitmap = New Bitmap(objGraphic, CInt(objGraphic.Width * ratio), CInt(objGraphic.Height * ratio))

Edit: Probably need to explicitly convert new height and width to ints

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文