How to compress to better quality RRS feed

  • Question

  • User-296603942 posted

    Hi all,

    I have succeeded in getting an image to become resized (in kB) in case it would be too large, see code below. However, I am not really impressed with the loss of quality and wonder if there is a better way to tweak the parameters.

    What my code oes is to take the image file, make a bitmap of it, then compress it by reducing the image quality. If the file size is below a treshold value it will replace the old image. When I try this code, if I have a 750 kB image, it will go down to a quality percentage of about 50 before it reaches below 300 kB in size (which is my preferred value), and then the image doesn't look very appealing. Any ideas for tweaking?


     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim filnamn As String = "~\img\DSCtest.JPG"
            Bildkomprimering(filnamn, 300)
        End Sub
        Protected Sub Bildkomprimering(ByVal filnamn As String, ByVal maxkB As Integer)
            Dim objImage As Image
            Dim forstor As Boolean = True
            Dim procent As Integer = 80
                While forstor = True And procent > 5
                    objImage = Image.FromFile(Server.MapPath(filnamn))
                    Dim target As New Bitmap(objImage.Width, objImage.Height)
                    Dim e As Imaging.EncoderParameters
                    ' Jpeg image codec 
                    Dim jpegCodec As Imaging.ImageCodecInfo = Imaging.ImageCodecInfo.GetImageEncoders()(1)
                    e = New Imaging.EncoderParameters(2)
                    e.Param(0) = New Imaging.EncoderParameter(Imaging.Encoder.Quality, procent)
                    e.Param(1) = New Imaging.EncoderParameter(Imaging.Encoder.Compression, CLng(Imaging.EncoderValue.CompressionLZW))
                    Dim ms As IO.MemoryStream = New IO.MemoryStream
                    target.Save(ms, jpegCodec, e)
                    Dim tmpstorlek As Integer = ms.Length / 1024
                    'checking file size
                    If tmpstorlek > maxkB Then 'image too large
                        forstor = True
                        If tmpstorlek > 500 Then
                            procent /= 2 'just to skip some roundtrips
                            procent /= 1.3
                        End If
                        forstor = False
                        Dim fs As New FileStream(Server.MapPath(filnamn), FileMode.Create) : ms.WriteTo(fs) : fs.Close()
                    End If
                End While
                'Don't bother as it's not the end of the world if an image is too large
            End Try
        End Sub

    Friday, November 2, 2012 5:35 PM

All replies

  • User-1716253493 posted

    I think using default compression level value of photoshop/corelphotopaint is better.

    I am forget the level value, its about 10% till 30% i think.

    try open photoshop/photopaint save image to jpg and see the default compression level.

    Friday, November 2, 2012 8:40 PM