Io mi ritrovo spesso ad usare questo codice quando il server supporta asp.net
codice:
Public Sub ImgRedim(immagine as String, width as Integer, height as Integer, num as string, ext as string, size_img as string, cartella as string)
Dim bmp as System.Drawing.Bitmap = CType(System.Drawing.Image.FromFile(immagine), System.Drawing.Bitmap)
Dim name_thumb as string = num
if Convert.ToInt32(bmp.Height) > Height then
if height = 0 Then
height = Convert.ToInt32(width / bmp.Width * bmp.Height)
End if
if width = 0 Then
width = Convert.ToInt32(height / bmp.Height * bmp.Width)
End if
' CHECK DELLE DIMENSIONI DI INPUT ( MAXWIDTH 600, MAXHEIGHT 400
if width <= 600 and height > 400 then 'redimnesiona il width su height 400
height = 400
width = Convert.ToInt32(height/ bmp.Height * bmp.Width)
elseif width > 600 and height <= 400 then 'redimnesiona il width su height 400
width = 600
height = Convert.ToInt32(width / bmp.Width * bmp.Height)
end if
Dim miniaturabmp as New System.Drawing.Bitmap(bmp, width, height)
if immagine.EndsWith(".jpg") Then
miniaturabmp.Save(Server.MapPath("../writable/public/" & cartella & "/" & name_thumb & ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
else
miniaturabmp.Save(Server.MapPath("../writable/public/" & cartella & "/" & name_thumb & ".gif"), System.Drawing.Imaging.ImageFormat.Gif)
end if
miniaturabmp.Dispose()
else
if immagine.EndsWith(".jpg") Then
bmp.Save(Server.MapPath("../writable/public/" & cartella & "/" & name_thumb & ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg)
else
bmp.Save(Server.MapPath("../writable/public/" & cartella & "/" & name_thumb & ".gif"), System.Drawing.Imaging.ImageFormat.Gif)
end if
end if
Dim f As New FileInfo(server.MapPath("../writable/public/" & cartella & "/" & name_thumb & ext))
Dim info_bmp as System.Drawing.Bitmap = CType(System.Drawing.Image.FromFile(server.MapPath("../writable/public/" & cartella & "/" & name_thumb & ext)), System.Drawing.Bitmap)
Height = Convert.ToInt32(info_bmp.Height)
Width = Convert.ToInt32(info_bmp.Width)
info_bmp.Dispose()
if cartella = "grande" then
Dim_grande.text = "" & name_thumb & ext & " - Dimensioni: " & Width & "x" & Height & " Peso: " & FormatNumber(f.Length/1024, 1, , ,TriState.True) & " Kb"
elseif cartella = "piccola" then
Dim_piccola.text = "" & name_thumb & ext & " - Dimensioni: " & Width & "x" & Height & " Peso: " & FormatNumber(f.Length/1024, 1, , ,TriState.True) & " Kb"
end if
bmp.Dispose()
End Sub
'Esempio di chiamata
inputFile.PostedFile.SaveAs(Server.MapPath("../writable/public/" & num & ext)) 'num è un random per evitare che esista il file (magari un controllo + scrupoloso non guasterebbe :)
Dim size_img = inputFile.PostedFile.ContentLength
call ImgRedim(immagine, 100, 0, num, ext, size_img,"piccola")
call ImgRedim(immagine, 600, 0, num, ext, size_img,"grande")
Questo perchè nel backoffice delle persone invece di fargli creare
una miniatura (che salvo nell'esempio in writable/public/piccola/) ed una foto vera (che salvo nell'esempio in writable/public/grande/)
gli fai caricare una foto anche grossolana (sempre cmq jpg o gif) e poi ne creo una miniatura ed una che abbia i limiti imposti dal layout del sito (nel codice sopra cambia c'è un controlla che impone come massimo with 600 e height a 400)