.net 里可以轻松对图片文件进行质量压缩
look
'***********************************************************
'即时预览保存图片质量
'wgscd (c) 2005 QQ 153964481,Email: wgscd@126.com
Dim PulicImageCodecInfo As ImageCodecInfo '定义全局变量提高效率
Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo
Dim myEncoder As System.Drawing.Imaging.Encoder = System.Drawing.Imaging.Encoder.Quality
Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter
Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "保存文件"
Me.Label2.Text = "即时预览保存图片质量 wgscd (c) 02005 利用ImageCodecInfo,System.Drawing.Imaging.Encoder"
Me.HScrollBar1.Cursor = Cursors.Hand
PulicImageCodecInfo = GetEncoderInfo("image/jpeg") '获取ImageCodecInfo
Me.PictureBox2.Image.Save("c:\wgscd.jpg")
Me.PictureBox3.Image = Image.FromFile("c:\wgscd.jpg")
Dim fi As New FileInfo("c:\wgscd.jpg")
fi = New FileInfo("c:\wgscd.jpg")
Label1.Text = HScrollBar1.Value & "大小:" & (fi.Length / 1024).ToString("0.00") & "KB"
Me.PictureBox3.Refresh() '必须释放
Me.PictureBox3.Image.Dispose()
Me.PictureBox3.Image = Me.PictureBox2.Image
End Sub
Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll
' Dim MyMemoryStream As IO.MemoryStream = New IO.MemoryStream
myImageCodecInfo = PulicImageCodecInfo '用全局保存的ImageCodecInfo来设置
myEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1)
' myEncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, EncoderValue.TransformRotate90)
myEncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, CLng(Me.HScrollBar1.Value))
myEncoderParameters.Param(0) = myEncoderParameter
' Me.PictureBox2.Save(MyMemoryStream, myImageCodecInfo, myEncoderParameters)‘ 写如内存流
Try
Me.PictureBox2.Image.Save("c:\wgscd.jpg", myImageCodecInfo, myEncoderParameters)
Me.PictureBox3.Image = Image.FromFile("c:\wgscd.jpg")
Dim fi As New FileInfo("c:\wgscd.jpg")
fi = New FileInfo("c:\wgscd.jpg")
Label1.Text = " 保存质量: " & HScrollBar1.Value & " % 大小:" & (fi.Length / 1024).ToString("0.00") & "KB"
Me.PictureBox3.Refresh() '必须释放
Me.PictureBox3.Image.Dispose()
' myEncoderParameters.Dispose()
' MyMemoryStream.Flush()
Catch ex As Exception
'如果有必要处理错误,可加代码
' MsgBox(ex.ToString)
End Try
End Sub '
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
'可以获得系统的ImageEncoders
Sub GetImageEncoders()
Dim i As Integer
Dim encoders As System.Drawing.Imaging.ImageCodecInfo() = Imaging.ImageCodecInfo.GetImageEncoders
For i = 0 To (encoders.Length - 1)
MsgBox(encoders(i).MimeType)
Next i
End Sub
Private Function GetEncoderInfo(ByVal MYmimeType As String) As ImageCodecInfo
Dim i As Integer
Dim encoders As System.Drawing.Imaging.ImageCodecInfo() = Imaging.ImageCodecInfo.GetImageEncoders()
For i = 0 To (encoders.Length - 1)
If (encoders(i).MimeType = MYmimeType) Then
' MsgBox(encoders(i).MimeType)
Return encoders(i)
End If
Next i
End Function