Sandro Bizioli


Chi sogna di giorno conosce molte cose che sfuggono a chi sogna soltanto di notte. (E.A.Poe)
Mappa

Modificare il formato di un'immagine in VB.Net

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

Categoria: VB.Net
giovedì, 20 set 2007 Ore. 10.35
Statistiche
  • Views Home Page: 110.016
  • Views Posts: 561.002
  • Views Gallerie: 109.912
  • n° Posts: 227
  • n° Commenti: 222
Copyright © 2002-2007 - Blogs 2.0
dotNetHell.it | Home Page Blogs
ASP.NET 2.0 Windows 2003