Imports System.Drawing.Imaging
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim inImg As Image = Image.FromFile("C:\Temp\Images\DarkStout.jpg")
SaveImage("C:\Temp\Images\DarkStout1.jpg", inImg, "image/jpeg", 75, 24L)
End Sub
Private Sub SaveImage(ByVal filePath As String, ByVal img As Image, ByVal mimeType As String, ByVal quality As Long, ByVal bitDepth As Long)
If quality > 100 OrElse quality < 0 Then
Throw New ArgumentException("Valid qualities are between 0 and 100")
End If
Dim qualParam As New EncoderParameter(Encoder.Quality, quality)
Dim colorParam As New EncoderParameter(Encoder.ColorDepth, bitDepth)
Dim encoderParams As New EncoderParameters(2)
encoderParams.Param(0) = qualParam
encoderParams.Param(1) = colorParam
Dim ici As ImageCodecInfo = GetImageCodec(mimeType)
img.Save(filePath, ici, encoderParams)
End Sub
Private Function GetImageCodec(ByVal mimeType As String) As ImageCodecInfo
For Each ici As ImageCodecInfo In ImageCodecInfo.GetImageEncoders()
If ici.MimeType = mimeType Then
Return ici
End If
Next
Return Nothing
End Function
End Class