What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleSave a picture into a JPEG at a desired size in Visual Basic 2005
DescriptionThis example shows how to save a picture into a JPEG at a desired size in Visual Basic 2005.
Keywordssave picture, save image, size, jpeg, jpg, compress, VB .NET
CategoriesAlgorithms, VB.NET, Graphics
Function SaveJpegIntoStream saves an image into a JPEG encoding in a memory stream. It makes an EncoderParameters object and sets its first parameter to indicate that the file should be compressed to a given level. The function then calls GetEncoderInfo to get the encoder needed for saving JPEG files. It calls the image's Save method, passing it parameters to make it save into a memory stream using the JPEG encoder with the desired compression level.
' Save a JPEG in compressed form into a memory stream. 
' The compression_level value
' should be between 10 and 100.
Public Function SaveJpegIntoStream(ByVal image As Image, _
    ByVal compression_level As Long) As MemoryStream
    If compression_level < 10 Then
        Throw New ArgumentException("Compression level must " & _
            "be between 10 and 100")
    End If

    ' Get an encoder parameter array and set the
    ' compression level.
    Dim encoder_params As EncoderParameters = New _
    encoder_params.Param(0) = New _
        EncoderParameter(Encoder.Quality, compression_level)

    ' Prepare the codec to encode.
    Dim image_codec_info As ImageCodecInfo = _

    ' Save the file.
    Dim memory_stream As New MemoryStream()
    image.Save(memory_stream, image_codec_info, _

    Return memory_stream
End Function
Function GetEncoderInfo returns an encoder. It loops through the available encoders and returns the one that handles the desired type, in this example, JPEG files.
' Get a codec's information.
Private Function GetEncoderInfo(ByVal mimeType As String) _
    As ImageCodecInfo
    Dim encoders As ImageCodecInfo()
    encoders = ImageCodecInfo.GetImageEncoders()
    For i As Integer = 0 To encoders.Length
        If encoders(i).MimeType = mimeType Then
            Return encoders(i)
        End If
    Next i
    Return Nothing
End Function
The main program uses the following code to compress the image to a desired size. It loops the compression_level variable from 100 to 10, compressing the image into a memory stream for each compression_level value. When it finds a stream that is less than or equal to the desired size, it stops and saves the stream into a JPEG file.
Dim file_size As Long
For compression_level As Integer = 100 To 10 Step -1
    ' Save the file into a memory stream.
    Dim memory_stream As MemoryStream = _
        SaveJpegIntoStream(picOriginal.Image, _

    ' See how big it is.
    file_size = memory_stream.Length
    If file_size <= desired_size Then
        ' Display the final size and image.
        ' Save the file.
        My.Computer.FileSystem.WriteAllBytes(file_name, _
            memory_stream.ToArray(), False)
        picNew.Image = LoadImage(file_name)
        txtCompressionLevel.Text = compression_level
        txtCompressedSize.Text = FormatBytes(file_size)
        Exit For
    End If
Next compression_level
Note: Depending on the type of image you are compressing, the file may have the size you want even at a compression level of 100, which is the minimum compression. For example, you may be able to squeeze a lot of space out of a bitmap file without a lower compression level. The result can look very close to the original file but take up a lot less space.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.