All pastes #15026 Raw Edit

Save image, GDI+

public text v1 · immutable
#15026 ·published 2005-06-19 17:48 UTC
rendered paste body
'The sub that compresses and saves:
    Public Sub CompressImage(ByVal SourceFile As String, ByVal DestFile As String, ByVal MimeType As String, ByVal Quality As Integer)
        'Variables
        Dim myBitmap As Bitmap
        Dim myImageCodecInfo As ImageCodecInfo
        Dim myEncoder As Encoder
        Dim myEncoderParameter As EncoderParameter
        Dim myEncoderParameters As EncoderParameters

        myBitmap = New Bitmap(SourceFile)
        'Get the mimetype
        myImageCodecInfo = GetEncoderInfo(MimeType)
        'Set the quality
        myEncoder = Encoder.Quality
        myEncoderParameters = New EncoderParameters(1)
        myEncoderParameter = New EncoderParameter(myEncoder, Quality)
        myEncoderParameters.Param(0) = myEncoderParameter
        'Save the new image
        myBitmap.Save(DestFile, myImageCodecInfo, myEncoderParameters)
    End Sub

'The sub that loads the image to a PictureBox
    Public Sub LoadImage(ByVal Picturebox As PictureBox, ByVal ImagePath As String)
        Picturebox.Image = Image.FromFile(ImagePath)
    End Sub

'Trying to set the quality, save the image and reload it to a picturebox working as a preview:
'myImagePath and myImageTempPath are just variables.
    CompressImage(myImagePath, myImageTempPath, "image/jpeg", cbQualLev.Text)
    LoadImage(pbPreview, myImageTempPath)