Upload d'image : erreur en distant
Bonjour bonjour !
Alors voila. J'ai une super fonction (en fait deux) qui me permettent d'uploader une image sur le serveur et de la redimmensionner. Le seul souci, c'est que ça fonctionne très bien en local, mais en distant c'est niet.
Voici la fonction appelée en première :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| Public Function UploadAvatar() As Boolean
If IsPostBack Then
Dim path As String = Server.MapPath("~/UpImages/")
Dim fileOK As Boolean = False
If UploadPhoto.HasFile Then
Dim fileExtension As String
fileExtension = System.IO.Path.GetExtension(UploadPhoto.FileName).ToLower()
Dim allowedExtensions As String() = {".jpg", ".jpeg"}
For i As Integer = 0 To allowedExtensions.Length - 1
If fileExtension = allowedExtensions(i) Then
fileOK = True
End If
Next
If fileOK Then
Try
Dim oImage As System.Drawing.Image = Nothing
If File.Exists(path & "139.jpg") Then
File.Delete(path & "139.jpg")
ElseIf File.Exists(path & "139.jpeg") Then
File.Delete(path & "139.jpeg")
End If
Dim image As String = ""
If System.IO.Path.GetExtension(UploadPhoto.FileName).ToLower() = ".jpg" Then
UploadPhoto.PostedFile.SaveAs(path & _
"139.jpg")
oImage = System.Drawing.Image.FromFile(path & "139.jpg")
image = "139.jpg"
ElseIf System.IO.Path.GetExtension(UploadPhoto.FileName).ToLower() = ".jpeg" Then
UploadPhoto.PostedFile.SaveAs(path & _
"139.jpeg")
image = "139.jpeg"
oImage = System.Drawing.Image.FromFile(path & "139.jpeg")
Else
Dim ex As New Exception("Format non supporté, choisissez une autre photo.")
Throw ex
End If
PhotoEnregistreRetaille(oImage, System.IO.Path.GetExtension(UploadPhoto.FileName).ToLower(), image, 240, 200) 'Appel de la deuxième fonction
LblErreur.Text = "Image téléchargée !<br />Votre image sera redimensionnée si ses dimensions sont supérieurs à 240 X 200 pixels.<br />"
Return False
Catch ex As Exception
If ex.Message = "Format non supporté, choisissez une autre photo." Then
LblErreur.Text = ex.Message
Else
LblErreur.Text = "Ce fichier ne peut pas être téléchargé." 'C'est ce message d'erreur qui est affiché, donc c'est une exception qui est lancée.
End If
Return True
End Try
Else
LblErreur.Text = "Ce fichier ne peut pas être téléchargé."
Return True
End If
End If
End If
End Function |
Et voici la fonction qui permet de retailler l'image, appelée par la fonction précédente :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| Private Function PhotoEnregistreRetaille(ByVal oImageSource As System.Drawing.Image, ByVal Format As String, ByVal filename As String, ByVal LargeurMaxi As Integer, ByVal hauteurMaxi As Integer) As Boolean
Dim HauteurImage, LargeurImage As Integer
Dim propor As Double
If oImageSource.Width > LargeurMaxi Then
propor = oImageSource.Width / oImageSource.Height
LargeurImage = LargeurMaxi
HauteurImage = LargeurImage / propor
If HauteurImage > hauteurMaxi Then
HauteurImage = hauteurMaxi
LargeurImage = HauteurImage * propor
End If
Try
Dim oImageCible As System.Drawing.Bitmap = New System.Drawing.Bitmap(LargeurImage, HauteurImage, oImageSource.PixelFormat)
Dim oGraphique As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(oImageCible)
oGraphique.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
oGraphique.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
oGraphique.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality
oGraphique.DrawImage(oImageSource, 0, 0, oImageCible.Width, oImageCible.Height)
oGraphique.Dispose()
Dim ImgFormat As System.Drawing.Imaging.ImageFormat
Select Case Format
Case ".jpg"
ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg
Case ".jpeg"
ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg
Case Else
ImgFormat = System.Drawing.Imaging.ImageFormat.Jpeg
End Select
oImageSource.Dispose()
Dim path As String = Server.MapPath("~/UpImages/")
File.Delete(path & filename)
oImageCible.Save(path & filename, ImgFormat)
Return True
Catch exThread As Threading.ThreadAbortException
Throw exThread
Catch ex As Exception
Return False
End Try
Else
Return True
End If
End Function |
Si quelqu'un pouvait m'aider, ça serait génial. Ca fait déjà quelques heures que je suis dessus et toujours rien...:evilred:
Merci d'avance !