logo  Converting Image File Formats
This Chapter
Convert Graphics
Image Attribute
Special Effects
Picture Boxes
Rotate & Flip
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
These functions will convert image files from one image format to another. They all read an image file (which can be .bmp, .gif, .jpg, .png or .tif and write a file to the same directory in the new image format. Most of these conversions rely on

   img.Save(fileout, System.Drawing.Imaging.ImageFormat.___)
  
All the functions require one parameter - the full file name (including path) of the original image file
The .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