Henry Liang's Dot Net Blog

  Genius is one percent inspiration and ninety-nine percent perspiration.

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

今天花了半个小时写出来的,用来给System.Drawing.Image改变大小,而且在改变的同时保持原来的长宽比率。很简单,不过保存一下,以后还可以重复使用吧!
第一篇blog,自己给自己鼓励一下。

Private Sub ResizeImage(ByRef img As System.Drawing.Image, ByVal width As IntegerByVal height As IntegerByVal path As StringByVal 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
posted on 2006-06-10 05:20  Henry Liang  阅读(577)  评论(0编辑  收藏  举报