今天花了半个小时写出来的,用来给System.Drawing.Image改变大小,而且在改变的同时保持原来的长宽比率。很简单,不过保存一下,以后还可以重复使用吧!
第一篇blog,自己给自己鼓励一下。
Private Sub ResizeImage(ByRef img As System.Drawing.Image, ByVal width As Integer, ByVal height As Integer, ByVal path As String, ByVal format As System.Drawing.Imaging.ImageFormat)
Dim ratio As Double = (img.Width / img.Height)
If ratio >= (width / height) Then
Dim hd As String = (width / ratio).ToString()
Dim i As Integer = hd.IndexOf(Convert.ToChar("."))
If i > 0 Then
Dim h As Integer = Convert.ToInt16(hd.Remove(i, hd.Length - i))
height = h
Else
Dim h As Integer = Convert.ToInt16(hd)
height = h
End If
ElseIf ratio < (width / height) Then
Dim wd As String = (height * ratio).ToString()
Dim i As Integer = wd.IndexOf(Convert.ToChar("."))
If i > 0 Then
Dim w As Integer = Convert.ToInt16(wd.Remove(i, wd.Length - i))
width = w
Else
Dim w As Integer = Convert.ToInt16(wd)
width = w
End If
End If
Dim imgLarge As New Bitmap(img, width, height)
imgLarge.Save(path, format)
imgLarge.Dispose()
End Sub
Dim ratio As Double = (img.Width / img.Height)
If ratio >= (width / height) Then
Dim hd As String = (width / ratio).ToString()
Dim i As Integer = hd.IndexOf(Convert.ToChar("."))
If i > 0 Then
Dim h As Integer = Convert.ToInt16(hd.Remove(i, hd.Length - i))
height = h
Else
Dim h As Integer = Convert.ToInt16(hd)
height = h
End If
ElseIf ratio < (width / height) Then
Dim wd As String = (height * ratio).ToString()
Dim i As Integer = wd.IndexOf(Convert.ToChar("."))
If i > 0 Then
Dim w As Integer = Convert.ToInt16(wd.Remove(i, wd.Length - i))
width = w
Else
Dim w As Integer = Convert.ToInt16(wd)
width = w
End If
End If
Dim imgLarge As New Bitmap(img, width, height)
imgLarge.Save(path, format)
imgLarge.Dispose()
End Sub