VB.Net实现图片压缩的方法

    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 = "image/jpeg") Then
                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 & "123.jpg", ici, ep)
        compressPic.Dispose()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值