Ho provato il codice originale proposto da Veronica80, e ho provato a correggerlo, per quanto possibile.
a me sembra che vada.codice:Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load ridimensiona("prova.jpg", "c:\tmp", "c:\tmp\tmp", 600, 300) End Sub Private Sub ridimensiona(ByVal nomeImg As String, ByVal nomePath As String, ByVal destPath As String, ByVal wFinaleImg As Integer, ByVal hFinaleImg As Integer) Dim nomeCompletoImg As String = nomePath & "/" & nomeImg Dim IntXSize As Integer Dim IntYSize As Integer Dim IntHalfWidth As Integer Dim IntHalfHeight As Integer Dim X As Integer Dim Y As Integer Dim compressione As Long = 1 'Variabili per l'anteprima Dim ObjBMP As System.Drawing.Image Dim ObjGraphics As System.Drawing.Image 'prendiamo alcune informazioni importanti dall'immagine orginale ObjBMP = New Bitmap(nomeCompletoImg) 'Misure dell'immagine Dim IntImgW As Integer = ObjBMP.Width Dim IntImgH As Integer = ObjBMP.Height 'Nuove misure dell'immagine servono per mantenere le proporzioni, per il mio caso mi basta ridimensionare in base all'altezza Dim IntNewImgW As Integer Dim IntNewImgH As Integer IntNewImgH = hFinaleImg IntNewImgW = CInt((hFinaleImg / IntImgH) * IntImgW) 'ObjGraphics = ObjBMP.GetThumbnailImage(IntNewImgW, IntNewImgH, Nothing, IntPtr.Zero) ObjGraphics = New System.Drawing.Bitmap(ObjBMP, IntNewImgW, IntNewImgH) Dim myImageCodecInfo As System.Drawing.Imaging.ImageCodecInfo Dim myEncoder As System.Drawing.Imaging.Encoder Dim myEncoderParameter As System.Drawing.Imaging.EncoderParameter Dim myEncoderParameters As System.Drawing.Imaging.EncoderParameters Dim i As Integer Dim encoders As System.Drawing.Imaging.ImageCodecInfo() = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders() For i = 0 To (encoders.Length - 1) If (encoders(i).MimeType = "image/jpeg") Then myImageCodecInfo = encoders(i) End If Next i myEncoder = System.Drawing.Imaging.Encoder.Quality myEncoderParameters = New System.Drawing.Imaging.EncoderParameters(1) myEncoderParameter = New System.Drawing.Imaging.EncoderParameter(myEncoder, compressione) myEncoderParameters.Param(0) = myEncoderParameter 'ObjGraphics = ObjBMP.GetThumbnailImage(IntNewImgW, IntNewImgH, Nothing, IntPtr.Zero) 'Qui viene fatto il crop, se non vi è necessario saltatelo Dim ObjSysDrawImg As New System.Drawing.Bitmap(wFinaleImg, hFinaleImg, PixelFormat.Format24bppRgb) Dim ObjGraphics2 As Graphics = Graphics.FromImage(ObjSysDrawImg) ObjGraphics2.CompositingQuality = Drawing2D.CompositingQuality.HighQuality ObjGraphics2.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias ObjGraphics2.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic IntXSize = ObjGraphics.Width - 1 IntYSize = ObjGraphics.Height - 1 IntHalfWidth = CInt(IntXSize / 2) IntHalfHeight = CInt(IntYSize / 2) X = CInt(IntHalfWidth - (wFinaleImg / 2)) Y = CInt(IntHalfHeight - (hFinaleImg / 2)) ObjGraphics2.DrawImage(ObjGraphics, New Rectangle(0, 0, wFinaleImg, hFinaleImg), New Rectangle(X, Y, wFinaleImg, hFinaleImg), GraphicsUnit.Pixel) ObjGraphics.Dispose() '------------------------------------------------------ If IO.Directory.Exists(destPath) = False Then IO.Directory.CreateDirectory(destPath) End If 'ObjSysDrawImg.Save(destPath & "\" & nomeImg, myImageCodecInfo, myEncoderParameters) ObjSysDrawImg.Save(destPath & "\" & nomeImg) ObjBMP.Dispose() ObjGraphics2.Dispose() ObjSysDrawImg.Dispose() End Sub![]()
ps. La nostra bella lingua non ha bisogno di termini orribili quali croppare.![]()

Ho provato il codice originale proposto da Veronica80, e ho provato a correggerlo, per quanto possibile.
Rispondi quotando