VB.Net下图片压缩有几种方式可以实现:通过缩小图片大小;通过降低图片质量;其他专门的压缩工具或者组件(据说一些组件可以实现无损压缩)。本文主要介绍前面两种有损的VB.Net图片减肥方法。 第一种: 缩小图片大小 代码如下:
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 = \ ici = codec End If Next
ep.Param(0) = New EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 66) '此处参数66代表压缩率,建议设置65-85之间, 范围0-100 , 100代表图片质量最优,但压缩率最低
PictureBox1.Image.Save(System.Environment.CurrentDirectory & \ compressPic.Dispose()

