Const Int_SmallSize As Integer = 800 '这里可以自定义缩小后的图片的最大高度/宽度
'这里预设了800 代表宽与高最大值都不能超800象素
'功能函数中的MaxPic参数就是我们需要缩小的图片信息
Private Function GetMinPic(ByVal MaxPic As System.Drawing.Image) As System.Drawing.Image
If MaxPic.Height > Int_SmallSize Or MaxPic.Width > Int_SmallSize Then If MaxPic.Height > MaxPic.Width Then MinWidth = MaxPic.Width / (MaxPic.Height / Int_SmallSize) MinHeight = Int_SmallSize Else MinWidth = Int_SmallSize MinHeight = MaxPic.Height / (MaxPic.Width / Int_SmallSize) End If Return MaxPic.GetThumbnailImage(CInt(MinWidth), CInt(MinHeight), Nothing, New System.IntPtr()) Else Return MaxPic End If
End Function
第二种:牺牲质量来实现图片减肥
Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders() Dim ici As ImageCodecInfo = Nothing Dim codec As ImageCodecInfo
Dim compressPic As New Bitmap(PictureBox1.Image) Dim ep As EncoderParameters = New EncoderParameters() For Each codec In codecs If (codec.MimeType = "image/jpeg") Then ici = codec End If Next ep.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 66)