How to save a jpeg image with 50% quality.

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

I know many wonder how those good image editors save images with f.ex 50% quality.
A normal image (in vb) can be saved to a stream or a file. Here is the possible ways to save to a file:
Code: Select all
Image.Save(filename As String)
Image.Save(filename As String, ImageFormat)
Image.Save(filename As String, imageCodec as ImageCodecInfo, encoderParams As EncoderParameters)
You will be learning about the last one.
Remember to import the System.Drawing.Imaging namespace

So lets say we have an image called someimage, we want to save this image with 75% quality. We need to get the right image codec, and we need to create the encoder parameters. We could create a method to get the right codec. It would be like this:
Code: Select all
    Function GetImageCodec(ByVal mimeType As String) As ImageCodecInfo
        'Stores the avaliable codec infos in an array
        Dim codecs() As ImageCodecInfo = ImageCodecInfo.GetImageEncoders
        Dim theRightOne As ImageCodecInfo
        For codecIndex = 0 To codecs.Length - 1
            'Check if it is the right codec
            If codecs(codecIndex).MimeType = mimeType Then
                'This matches the codec we want
                theRightOne = codecs(codecIndex)
                Exit For
            End If
        Next

        'Cleanup
        Array.Clear(codecs, 0, codecs.Length)
        Return theRightOne
    End Function
The mimetype for JPEG is image/jpeg.
We also need to create the encoder parameters. That can be done easy.
Code: Select all
        Dim EncoderParameters As New EncoderParameters(1) 'Store 1 parameter
        EncoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, 75) 'Creates an EncoderParameter with the Quality encoder and sets it value to 75 (%)
Remember that the mimeType for the JPEG codec is image/jpeg. Now we can save our image with this little piece of code:
Code: Select all
    Function SaveImageWithQuality(ByVal image As Image, ByVal filename As String, ByVal Quality As Integer)
        If Quality < 0 OrElse Quality > 100 Then
            Throw New ArgumentOutOfRangeException("The quality must be between 0 and 100")
            Exit Function
        End If

        Dim EncoderParameters As New EncoderParameters(1) 'Store 1 parameter
        EncoderParameters.Param(0) = New EncoderParameter(Encoder.Quality, Quality) 'Creates an EncoderParameter with the Quality encoder and sets it value from the 'Quality' parameter

        Dim codec As ImageCodecInfo = GetImageCodec("image/jpeg")

        image.Save(filename, codec, EncoderParameters) 'Saves the image to 'filename' with the jpeg encoder and the quality from the 'quality' parameter
    End Function
Code: Select all
SaveImageWithQuality(someimage, "Test.jpg", 50)
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Awesome tut :D +rep
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

thx :D though i dont think you can +rep me since you already have
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
XTechVB
VIP - Site Partner
VIP - Site Partner
Posts: 727
Joined: Thu May 20, 2010 10:32 am

Nice tutorial dude! +rep
You can find me on Facebook or on Skype mihai_92b
User avatar
Napster1488
VIP - Donator
VIP - Donator
Posts: 524
Joined: Fri Jan 07, 2011 8:41 pm

Good work,
i never tought it can be done in this Way.
YouTube Downloader v3.0
Image
Image
Image
5 posts Page 1 of 1
Return to “Tutorials”