如何在VB.Net中重新调整图像大小

时间:2015-06-05 13:49:31

标签: vb.net

我需要它,以便在我的代码中,如果某些内容评估为true,则会更改图像的位置和大小。

到目前为止,这是我的代码:

With picValueTwentySix
   .Location = New Point(302, 134)
   .Size = New System.Drawing.Size(169, 40)
   .SizeMode = PictureBoxSizeMode.Zoom
End With

有人知道为什么不重新调整尺寸吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

正如大家已经提到的,你需要处理图像。这是一个我易于使用的功能。

Public Function ResizeImage(ByVal image As Image, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
    Try
        Dim newWidth As Integer
        Dim newHeight As Integer
        If preserveAspectRatio Then
            Dim originalWidth As Integer = image.Width
            Dim originalHeight As Integer = image.Height
            Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
            Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
            Dim percent As Single = IIf(percentHeight < percentWidth, percentHeight, percentWidth)
            newWidth = CInt(originalWidth * percent)
            newHeight = CInt(originalHeight * percent)
        Else
            newWidth = size.Width
            newHeight = size.Height
        End If

        Dim newImage As Image = New Bitmap(newWidth, newHeight)
        Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
            graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
            graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
        End Using

        Return newImage

    Catch ex As Exception
        Return image
    End Try
End Function

基本上它会根据您请求的尺寸创建一个新的空白图形,然后将原始图像复制到它,同时缩放它以适合它。我想如果你一步一步地抛出一条线你应该是非常自我解释的,但问你是否有问题......

答案 1 :(得分:1)

如@Plutonix所述,更改Picturebox尺寸不会影响图像尺寸本身,您必须确保实际图像尺寸大于图片框的尺寸,设置尺寸模式对于 stretchimage 的图片框,在这种情况下,一旦你调整图片框的大小,变化就会反映出来。调整大小后也刷新图片框。