allego la funzione ReDimImg
codice:
Public Sub ReDimImg(SourcePath as String,ResizeName as string, width as integer,height as integer)
Try
'Creo l'oggetto di riferimento al file originale
Dim TmpBmp as System.Drawing.Bitmap = CType(System.Drawing.Image.FromFile(Server.MapPath(SourcePath)), System.Drawing.Bitmap)
Dim newWidth,newHeight as string
'Se il parametro height è lasciato a 0 costruisco il with in maniera proporzionata
if height = 0 Then
newHeight = Convert.ToInt32(width / TmpBmp.Width * TmpBmp.Height)
else
newHeight = height
End if
'Se il parametro with è lasciato a 0 costruisco l'height in maniera proporzionata
if width = 0 Then
newWidth = Convert.ToInt32(height / TmpBmp.Height * TmpBmp.Width)
else
newWidth = width
end if
'Creo la miniatura con lunghezza e larghezza elaborati
Dim miniaturabmp as New System.Drawing.Bitmap(TmpBmp, newWidth, newHeight)
'Distruggo l'oggetto dell'immagine originale che non mi serve +
TmpBmp.Dispose()
'controllo l'estensione e salvo la miniatura nel giusto formato
if SourcePath.toLower().EndsWith(".jpg") or SourcePath.toLower().EndsWith(".jpeg") Then
miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.Jpeg)
elseif SourcePath.toLower().EndsWith(".gif") Then
miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.Gif)
elseif SourcePath.toLower().EndsWith(".png") Then
miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.Png)
else
Response.Write("Errore Formato")
'Si puo' ampliare l'elseif in questo modo:
'elseif SourcePath.toLower().EndsWith(".[Estensione]") Then
'miniaturabmp.Save(Server.MapPath(ResizeName), System.Drawing.Imaging.ImageFormat.[Estensione])
'dove [Estensione] puo essere Bmp, Emf, Exif, Tiff, Wmf
end if
miniaturabmp.Dispose()
Catch ex as System.Exception
'Nel caso stampo l'errore:
Response.Write("<hr>Errore nel ridimensionamento dell'immagine:
" & ex.Message & "
")
Response.Write("
" & ex.StackTrace & "<hr>")
End Try
Response.Write("Ridimensionamento effettuato con successo")
End Sub