日期:2010-05-09  浏览次数:20656 次

'***********************************************************
    '即时预览保存图片质量
    '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") &