J'ai un fichier image téléchargé par un upload.
L'objet est un stream.
Je voudrais redimensionner l'image avant de l'enregistrer dans SQL
Mais le stream n'est pas changé car je n'arrive pas à transformer mon bitmap (nécessaire pour changer la taille de l'image) en stream. C'est la fonction
RedimensionneImage qui pose problème
Voici le code :
'On passe la hauteur max, la largeur max, et le fichier type System.IO.Stream (System.IO.HttpStream)
clsImage.VerifieTailleImage(clsConstantes.i_MAXHEIGHT_IMG_CULTURE, clsConstantes.i_MAXWIDTH_IMG_CULTURE, e.UploadedFile.InputStream)
Shared Sub VerifieTailleImage(ByVal iMaxHeigth As Integer, _
ByVal iMaxWidth As Integer, _
ByRef ObjImg As Object)
Dim iCoefficient As Double = 0
Dim iCoefficientHeight As Double = 0
Dim iCoefficientWidth As Double = 0
Dim iNewHeight As Integer = 0
Dim iNewWidth As Integer = 0
Dim iHeightImg As Integer = 0
Dim iWidthImg As Integer = 0
Dim imgClientSize As Size = New System.Drawing.Size(0, 0)
imgClientSize = CalculeTailleImage(ObjImg)
iHeightImg = imgClientSize.Height
iWidthImg = imgClientSize.Width
If Not imgClientSize.IsEmpty AndAlso (iHeightImg > iMaxHeigth Or iWidthImg > iMaxWidth) Then
iCoefficientHeight = iMaxHeigth / iHeightImg
iCoefficientWidth = iMaxWidth / iWidthImg
If iCoefficientHeight <= iCoefficientWidth Then
iCoefficient = iCoefficientHeight
Else
iCoefficient = iCoefficientWidth
End If
iNewHeight = CInt(iHeightImg * iCoefficient)
iNewWidth = CInt(iWidthImg * iCoefficient)
ObjImg = RedimensionneImage(iNewHeight, iNewWidth, ObjImg)
End If
End Sub
Shared Function CalculeTailleImage(ByVal ObjImg As Object) As Size
Dim b As System.Drawing.Bitmap = Nothing
Dim imgClientSize As Size = New System.Drawing.Size(0, 0)
Select Case ObjImg.GetType().Name
Case GetType(Image).Name
b = New System.Drawing.Bitmap(CType(ObjImg, Image))
imgClientSize = New System.Drawing.Size(b.Size.Width, b.Size.Height)
Case GetType(ImageButton).Name
imgClientSize = New System.Drawing.Size(CType(ObjImg, ImageButton).Height.Value, CType(ObjImg, ImageButton).Width.Value)
Case Else
If ObjImg.GetType.Name.Contains(GetType(System.IO.Stream).Name) Then
b = New System.Drawing.Bitmap(CType(ObjImg, System.IO.Stream))
imgClientSize = New System.Drawing.Size(b.Size.Width, b.Size.Height)
End If
End Select
Return imgClientSize
End Function
Shared Function RedimensionneImage(ByVal iNewHeight As Integer, _
ByVal iNewWidth As Integer, _
ByRef ObjImg As Object) As Object
Select Case ObjImg.GetType().Name
Case GetType(Image).Name
Dim b As System.Drawing.Bitmap = New System.Drawing.Bitmap(CType(ObjImg, Image))
Dim Newb As System.Drawing.Bitmap = New System.Drawing.Bitmap(iNewWidth, iNewHeight, b.PixelFormat)
b = Newb
Dim objNewImage As Image = CType(ObjImg, Image)
objNewImage = New System.Drawing.Bitmap(b)
ObjImg = objNewImage
Case GetType(ImageButton).Name
CType(ObjImg, ImageButton).Height = System.Web.UI.WebControls.Unit.Parse(iNewHeight)
CType(ObjImg, ImageButton).Width = System.Web.UI.WebControls.Unit.Parse(iNewWidth)
Case Else
If ObjImg.GetType.Name.Contains(GetType(System.IO.Stream).Name) Then
Dim s As System.IO.Stream = CType(ObjImg, System.IO.Stream)
Dim b As System.Drawing.Bitmap = New System.Drawing.Bitmap(s)
Dim Newb As System.Drawing.Bitmap = New System.Drawing.Bitmap(iNewWidth, iNewHeight, b.PixelFormat)
b = Newb
ObjImg = b End If
End Select
Return ObjImg
End Function
Quelqu'un peut-il m'aider ?
Merci d'avance
Blowdesign