Summary: [VB] Image is proportionally reduced method
The method of proportionally reduced image
‘Set the image length and width
Public Sub setImageWH()
Try
‘Picture location
Dim FilePath As String = "~/Images/Img.png"
‘Get the real path of the picture
Dim realPath As String = HttpContext.Current.Request.MapPath(FilePath)
‘
Dim ImgDw As System.Drawing.Image = System.Drawing.Image.FromFile(realPath)
‘The length and width of the thumbnail after getting
Dim imgSize() As Integer = getThumbnailImageScale(120, 100, ImgDw.Width, ImgDw.Height)
ImgDw.Dispose()
‘Set image length and width
imgTest.Width = imgSize(0)
imgTest.Height = imgSize(1)
Catch ex As Exception
End Try
End Sub
‘Use when saving
Private Sub imgresize(ByVal width As Integer, ByVal height As Integer, ByVal saveurl As String, ByVal filebytes As Byte(), ByVal filelength As Integer)
Dim ms As MemoryStream = New MemoryStream
ms.Write(filebytes, 0, filelength)
ms.Flush()
Dim img1 As System.Drawing.Image = System.Drawing.Image.FromStream(ms)
If img1.Height> height Or img1.Width> width Then
‘Is there a super height or width?
Dim newimg As System.Drawing.Image
Dim thumbnailScale As Integer() = getThumbnailImageScale(width, height, img1.Width, img1.Height)
‘Calculate the aspect ratio of the original image
newimg = img1.GetThumbnailImage(thumbnailScale(0), thumbnailScale(1), Nothing, IntPtr.Zero)
‘Release resources
newimg.Save(saveurl)
newimg.Dispose()
img1.Dispose()
ms.Close()
End If
End Sub
‘Bring the length and width of the restricted range to the original image
Private Function getThumbnailImageScale(ByVal maxWidth As Integer, ByVal maxHeight As Integer, ByVal oldWidth As Integer, ByVal oldHeight As Integer) As Integer()
Dim result() As Integer = New Integer() {0, 0}
Dim widthDividend As Single, heightDividend As Single, commonDividend As Single
widthDividend = oldWidth / maxWidth
heightDividend = oldHeight / maxHeight
If (heightDividend> widthDividend) Then
commonDividend = heightDividend
Else
commonDividend = widthDividend
End If
result(0) = CType((oldWidth / commonDividend), Integer)
result(1) = CType((oldHeight / commonDividend), Integer)
Return result
End Function
If there are errors or incorrect ideas in the above article, please feel free to advise.
If there is any infringement, please respond to me~ Thank you:)
Original text: Large column [VB] Image scaling method