My Blog List

Tuesday, August 6, 2013

Convert Image Formats

You could face situations like changing the format of the image file or reduce the file size by changing the formats. Here is the way to get it done in simple way.

Function ConvertImage(strFromFile, strToFile)
 Dim oImage, oFileImage, oImageFormats, oFSO, strToExtn

 Set oFSO = CreateObject("Scripting.FileSystemObject")
 Set oImage = DotNetFactory.CreateInstance("System.Drawing.Image")
 Set oFileImage =  oImage.FromFile(strFromFile)
 Set oImageFormats = DotNetFactory.CreateInstance("System.Drawing.Imaging.ImageFormat","System.Drawing", Nothing)

 strToExtn = oFSO.GetExtensionName(strToFile)
 Select Case UCase(Trim(strToExtn))
  Case "JPEG"
   oFileImage.Save strToFile, oImageFormats.Jpeg
  Case "JPG"
   oFileImage.Save strToFile, oImageFormats.Jpeg
  Case "BMP"
   oFileImage.Save strToFile, oImageFormats.Bmp
  Case "PNG"
   oFileImage.Save strToFile, oImageFormats.Png
  Case "GIF"
   oFileImage.Save strToFile, oImageFormats.Gif
 End Select

 Set oFSO = Nothing
 Set oImage = Nothing
 Set oFileImage = Nothing
 Set oImageFormats = Nothing
End Function

Call ConvertImage("C:\sample.bmp", "C:\sample.jpeg")