img.Save(fileout, System.Drawing.Imaging.ImageFormat.___)
All the functions require one parameter - the full file name (including path) of the original image fileThe .JPG image format is a "lossy format," it sacrifices image quality in order to reduce the .jpg file size. The conversion function requires an additional parameter "quality" ( 0 to 100) which indicates the correct balance between file size and image accuracy. 0=keep file as small as possible and accept picture will be attrocious quality. 100=Do not sacrifice any image quality and accept that the image file will be very large. If unsure what quality is required, try starting around 75 and adjust until both picture quality and file size are acceptable.
Private Function Convert_To_Bmp(filename As String) As String
Try
If IO.Path.GetExtension(filename).Contains(".bmp", StringComparison.InvariantCultureIgnoreCase) Then
Exit Try
End If
Dim pat As String = IO.Path.GetDirectoryName(filename)
If Mid(pat, pat.Length, 1) <> "\" Then pat &= "\"
Dim fileout As String = pat & IO.Path.GetFileNameWithoutExtension(filename) & ".bmp"
Dim img As Image = Image.FromFile(filename)
img.Save(fileout, System.Drawing.Imaging.ImageFormat.Bmp)
Catch ex As Exception
Return ex.Message
End Try
Return String.Empty
End Function
Private Function Convert_To_Gif(filename As String) As String
Try
If IO.Path.GetExtension(filename).Contains(".gif", StringComparison.InvariantCultureIgnoreCase) Then
Exit Try
End If
Dim pat As String = IO.Path.GetDirectoryName(filename)
If Mid(pat, pat.Length, 1) <> "\" Then pat &= "\"
Dim fileout As String = pat & IO.Path.GetFileNameWithoutExtension(filename) & ".gif"
Dim img As Image = Image.FromFile(filename)
img.Save(fileout, System.Drawing.Imaging.ImageFormat.Gif)
Catch ex As Exception
Return ex.Message
End Try
Return String.Empty
End Function
Private Function Convert_To_Jpg(filename As String, quality As Integer) As String
If quality < 10 OrElse quality > 100 Then quality = 100
Try
If IO.Path.GetExtension(filename).Contains(".jp", StringComparison.InvariantCultureIgnoreCase) Then
Exit Try
End If
Dim pat As String = IO.Path.GetDirectoryName(filename)
If Mid(pat, pat.Length, 1) <> "\" Then pat &= "\"
Dim fileout As String = pat & IO.Path.GetFileNameWithoutExtension(filename) & ".jpg"
Dim Params As New System.Drawing.Imaging.EncoderParameters(1)
Params.Param(0) = New System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality)
Dim img As Image = Image.FromFile(filename)
Dim info As System.Drawing.Imaging.ImageCodecInfo()
info = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders
img.Save(fileout, info(1), Params)
Catch ex As Exception
Return ex.Message
End Try
Return String.Empty
End Function
Private Function Convert_To_Png(filename As String) As String
Try
If IO.Path.GetExtension(filename).Contains(".png", StringComparison.InvariantCultureIgnoreCase) Then
Exit Try
End If
Dim pat As String = IO.Path.GetDirectoryName(filename)
If Mid(pat, pat.Length, 1) <> "\" Then pat &= "\"
Dim fileout As String = pat & IO.Path.GetFileNameWithoutExtension(filename) & ".png"
Dim img As Image = Image.FromFile(filename)
img.Save(fileout, System.Drawing.Imaging.ImageFormat.Png)
Catch ex As Exception
Return ex.Message
End Try
Return String.Empty
End Function
Private Function Convert_To_Tif(filename As String) As String
Try
If IO.Path.GetExtension(filename).Contains(".tif", StringComparison.InvariantCultureIgnoreCase) Then
Exit Try
End If
Dim pat As String = IO.Path.GetDirectoryName(filename)
If Mid(pat, pat.Length, 1) <> "\" Then pat &= "\"
Dim fileout As String = pat & IO.Path.GetFileNameWithoutExtension(filename) & ".tif"
Dim img As Image = Image.FromFile(filename)
img.Save(fileout, System.Drawing.Imaging.ImageFormat.Tiff)
Catch ex As Exception
Return ex.Message
End Try
Return String.Empty
End Function
DigitalDan.co.uk