Attraverso lo spazio dei nomi System.Drawing.Imaging è possibile specificare il formato di un'immagine.
La classe è supportata in .Net Framework 3.0, 2.0, 1.1, 1.0
La funzione riportata accetta un oggetto Image ed una stringa indicante il nome completo del file dove salvare l'immagine nel formato scelto.
La discriminazione del formato è data dell'estensione stessa del nome del file.
Private Function SaveImage(ByVal img As Image, ByVal sFile As String) As Boolean
'
' Verifico di aver passato il nome del file
'
If sFile Is Nothing Then Return False
Dim fmt As Imaging.ImageFormat
Dim lFile As String = sFile.ToLower
'
' Verifico l'estensione del file
'
If lFile.EndsWith(".emf") Then
fmt = Imaging.ImageFormat.Emf
ElseIf lFile.EndsWith(".exif") Then
fmt = Imaging.ImageFormat.Exif
ElseIf lFile.EndsWith(".gif") Then
fmt = Imaging.ImageFormat.Gif
ElseIf lFile.EndsWith(".jpg") OrElse lFile.EndsWith(".jpeg") Then
fmt = Imaging.ImageFormat.Jpeg
ElseIf lFile.EndsWith(".png") Then
fmt = Imaging.ImageFormat.Png
ElseIf lFile.EndsWith(".tiff") OrElse lFile.EndsWith(".tif") Then
fmt = Imaging.ImageFormat.Tiff
ElseIf lFile.EndsWith(".wmf") Then
fmt = Imaging.ImageFormat.Wmf
Else
fmt = Imaging.ImageFormat.Bmp
End If
'
' Effettuo il salvataggio fisico dell'immagine
'
Try
img.Save(sFile, fmt)
SaveImage = True
Catch ex As Exception
MsgBox(Err.Description)
SaveImage = False
End Try
End Function