Di seguito un esempio che feci in 1.1 per files JPG, in una pagina di registrazione dell'utente. Ma attenzione alla qualità, ti ho postato quei link apposta.
Ma soprattutto, attenzione ai --permessi di scrittura--
codice:
Imports System.Drawing
Imports System.Drawing.Drawing2D
' ========================
' UPLOAD & RESIZE 100x100
' ========================
Dim PercorsoFoto As String
Dim NomeFileJPG As String
PercorsoFoto = <controllo_fileupload>.PostedFile.FileName
NomeFileJPG = System.IO.Path.GetFileName(PercorsoFoto)
<controllo_fileupload>.PostedFile.SaveAs(Request.MapPath("foto_utenti\") & NomeFileJPG)
Dim nuovagrandezza As New System.Drawing.Size
nuovagrandezza.Width = 100
nuovagrandezza.Height = 100
Dim ImmaginePiccola As Bitmap
ImmaginePiccola = ResizeImage(ImmagineGrande, nuovagrandezza, True)
ImmaginePiccola.Save(<tuopercorso> & <tuonomefoto> & "_THUMB.JPG")
' ========================
' Function per il resize
' ========================
Private Function ResizeImage(ByVal currentImage As System.Drawing.Image, ByVal newSize As Size, Optional ByVal MaintainAspect As Boolean = True) As System.Drawing.Image
If MaintainAspect = True Then
newSize = CalculateImageSize(currentImage.Size, newSize)
End If
Dim TargetBitmap As New Bitmap(currentImage, newSize)
Dim TargetGraphic As Graphics = Graphics.FromImage(TargetBitmap)
TargetGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic
TargetGraphic.SmoothingMode = SmoothingMode.HighQuality
TargetGraphic.DrawImage(currentImage, New Rectangle(0, 0, newSize.Width, newSize.Height), 0, 0, currentImage.Width, currentImage.Height, GraphicsUnit.Pixel)
TargetGraphic.Dispose()
TargetGraphic = Nothing
Return TargetBitmap
End Function
Private Function CalculateImageSize(ByVal ImageSize As Size, ByVal newsize As Size) As Size
Dim mySize As Size
Dim rNew As Double
Dim rCur As Double
rNew = newsize.Width / newsize.Height
rCur = ImageSize.Width / ImageSize.Height
If rCur > rNew Then
mySize.Width = newsize.Width
mySize.Height = CInt(mySize.Width / rCur)
End If
If rCur < rNew Then
mySize.Height = newsize.Height
mySize.Width = CInt(mySize.Height * rCur)
End If
If rCur = rNew Then
mySize.Width = newsize.Width
mySize.Height = newsize.Height
End If
Return mySize
End Function